specialized template function w/compiler error on invalid type

  • Thread starter chrisstankevitz
  • Start date
C

chrisstankevitz

Hi,

Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?
Q2: If not, how do people deal with this issue?

I believe A1 is no. I tried below with template function "f".

For A2, functors work but have ugly syntax and runtime overhead. A
dispatcher to the funtor has nice syntax but still the overhead. I'm
just guessing about the overhead, but I believe there is a ctor.

Thanks,

Chris

//==========


// f: template function defined only for char
template<typename T>
void f(T t);

template<>
void f(char i)
{
// ...
}

// g: template functor defined only for char
template<typename T>
struct g;

template<>
struct g<char>
{
void operator()(char i)
{
// ...
}
};

// h: dispatcher to g
template<typename T>
void h(T t)
{
g<T>()(t);
}

int main()
{
char c;
long l;

// f - template function w/specialization
// syntax: clean
// runtime: fast
// compiler error on invalid type: no (but linker error)
f(c);
f(l);

// g - template class w/specialization
// syntax: ugly
// runtime: slow
// compiler error on invalid type: yes
g<char>()(c);
g<long>()(l);

// h - Dispatcher function calling g
// syntax: clean
// runtime: slow
// compiler error on invalid type: yes (albeit cryptic)
h(c);
h(l);

return 0;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,

Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?

Yes. The general technique is to use SFINAE. The Boost library
provides ready-to-use helper classes for this.

Q2: If not, how do people deal with this issue?

N/A.
 
V

Victor Bazarov

Q1: Is there a way to make a template function that works only for
specific types which produces a compiler error if used with an invalid
type?

I am sorry, but this is just silly. Why do you need it to be a template
then? Drop the type from the template, replace it with your specific
type (for which you need it work) and be happy.
Q2: If not, how do people deal with this issue?

Normal people don't. See above.

V
 
C

chrisstankevitz

Victor said:
Why do you need it to be a template

I use the template facility to use a specialized implementation for 64
bit long. Overloading cannot do this AFAIK.
 
V

Victor Bazarov

I use the template facility to use a specialized implementation for 64
bit long. Overloading cannot do this AFAIK.

Just disregard my message; I failed to notice plural in your "types"
requirement. Sorry. I've already cancelled it...

V
 

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,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top