Forbidding template instantiation for specific type

  • Thread starter Marcin Kalicinski
  • Start date
M

Marcin Kalicinski

Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).

Best regards,
Marcin
 
B

Buster

Marcin said:
Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).

Error messages vary across implementations, but you can try

template <> void func (const char *)
{
int constraints_func_undefined_for_pointer_to_const_char [-1];
}

Regards,
Buster.
 
B

Buster

Marcin said:
Hi,

I am modernizing a small library that is being currently in use by some
people. In the new version I want to forbid use of a template function with
argument of const char * (i.e. I want to get compile errors when it
happens). For the rest of the types function should work ok.

// A template function, in old version of the library allowed for all types
template<typename T> void func(T t)
{
/* ... */
}

Now I want to forbid its use with const char *. For example I could use
specialization in the following way:

template<> void func(const char *)
{
const int a = 0;
a = 1;
}

Is there a cleaner way of achieving the same? Expecially to get better error
message? (ideally the error message should say that func(const char*) is
undefined).

Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.

Regards,
Buster.
 
M

Marcin Kalicinski

Uzytkownik "Buster said:
Oh man. That's too funny. Ignore my other post. Just don't define it!

template <> void func (const char *);

You'll get a link error.

Now that's a clever idea. Thanks!

Best regards,
Marcin
 
D

Dan Moos

Now that's a clever idea. Thanks!

Best regards,

I guess I don't see how that is an improvement over the OP's initial method.
Both will confuse an unsuspecting user of the template who isn't "In the
know". I other words, if I try to use that template with a const char *,
and I get a link error, I'm gonna be one confused dude, and can't imagine
coming to the conclusion that it is intended behaviour! I actually liked the
first idea of creating an array with an informative name, and giving it a
negative number as a size. I actually found that to be pretty clever! The
trick is giving the array a name that makes the situation obvious, like
"USER_this_template_does_not_work_with_const_char_pointers"
 
B

Buster

Dan said:
I guess I don't see how that is an improvement over the OP's initial method.
Both will confuse an unsuspecting user of the template who isn't "In the
know". I other words, if I try to use that template with a const char *,
and I get a link error, I'm gonna be one confused dude, and can't imagine
coming to the conclusion that it is intended behaviour! I actually liked the
first idea of creating an array with an informative name, and giving it a
negative number as a size. I actually found that to be pretty clever! The
trick is giving the array a name that makes the situation obvious, like
"USER_this_template_does_not_work_with_const_char_pointers"

Good point. We should use plain English. I was about to suggest
'function_func_is_not_implemented_for_arguments_of_type_pointer_to_const_char'
before I thought of the other idea. But the OP did ask for an error
saying that the function was undefined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top