By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

specialized template function w/compiler error on invalid type

P: n/a
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;
}

Nov 9 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* ch*************@yahoo.com:
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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 9 '06 #2

P: n/a
ch*************@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #3

P: n/a

Victor Bazarov wrote:
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.

Nov 9 '06 #4

P: n/a
ch*************@yahoo.com wrote:
Victor Bazarov wrote:
>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.
Just disregard my message; I failed to notice plural in your "types"
requirement. Sorry. I've already cancelled it...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '06 #5

P: n/a

Alf P. Steinbach wrote:
use SFINAE.
Wow, thanks. Google search showed me some amazing stuff.

Chris

Nov 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.