Template Argument Deduction Order [C++]

Started by
15 comments, last by SiCrane 14 years, 2 months ago
Well I made it specifically for function pointers as that's what I need it for.

Another question, why does this:

template<typename T, int n>void Func(){}template<typename T>void Func<T, 1>(){	int a = 0;	a++;}


Result in
error C2768: 'Func' : illegal use of explicit template arguments


I'm just trying to specialize it
Advertisement
Your second template line should be template<typename T, int n>, just like the first. What you've done there is define a template that only takes one parameter, so when you provide two parameters to the specialization, the compiler will complain.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

template<typename T, unsigned n>void Func(){}template<typename T, unsigned n>void Func<T, 1>(){	int a = 0;	a++;}


error C2995: 'void Func(void)' : function template has already been defined

Triggered on the second function.

EDIT:
Yet this seems to compile fine, something about the extra argument
template<unsigned T>void Func(){}template<>void Func<1>(){	int a = 0; a++;}
Eurgh, I totally spaced out that you're using template functions and not template classes... sorry!

This article should explain what's going on.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Ok thanks I got that working but yet another problem persists :(
template<typename T, typename A>struct Test{};template<typename T, typename A, typename B>struct Test{};


Templates can be damn annoying sometimes.
1>c:\users\tigeradmin\documents\visual studio 2008\projects\templatetest\templatetest\main.cpp(85) : error C2977: 'Test' : too many template arguments1>        c:\users\tigeradmin\documents\visual studio 2008\projects\templatetest\templatetest\main.cpp(79) : see declaration of 'Test'


It seems you can't partially specialize functions, but you can't overload structs - I'm stuck between two stones here.
Any reason you can't just do a single template<typename A, typename B, typename C = defaultCvalue> ?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Alternately, you can use a type list as your second template argument.

This topic is closed to new replies.

Advertisement