Re: template function specialisation
Den fredag den 28. februar 2014 13.37.32 UTC+1 skrev Victor Bazarov:
On 2/28/2014 4:15 AM, peter koch wrote:
I have a problem specializing a template function. Is it me or is it Microsoft?
I suspect it's you.
imagine:
template<typename T1,typename T2>
struct dummy {};
template<typename T>
void func() { }
template<>
void func<int>() { }
template<typename T1,typename T2>
void func<dummy<T1,T2>>() {}
For the second specialization I get
error : 'func' : illegal use of explicit template arguments
The information at
http://msdn.microsoft.com/en-us/library/e338tex6.aspx does not give
me an explanation I understand. It talks about partial specialization
of functions but is this it? I know how to work my way out of it, but
the above notation is more elegant to me.
You need to make up your mind about 'func'. It's either a template of
one argument, in which case you can put a single argument between the
angle brackets following the name of that function, like you did with
func< dummy<...> >
^^^^^^^^^^ That's the argument of 'func' template and it's a
*single* one.
, or it's a template of two arguments as you claim in the beginning of
the declaration:
template<typename T1, typename T2>
void...
Perhaps you misread my code?
f has one template argument. The two-argument template you quoted above is the definition of a struct.
What is it you're trying to accomplish?
Let us say I have a function. Mostly I would do one thing - say stream out the typename of the type. But for one particular template type - think std::pair - I would like to do something else - say stream out the typename of each type.
The real example is for accessing a GUI element, where I have a generic functionality for most types but a different functionality for a collection of elements (something similar to a std::vector). I just wanted to simplify the example given.
/Peter