My jaw dropped.......into a blender. A simple question leads to another rabbit hole, but at this point I shouldn't be surprised. My book showed one example of a template specialization for a class without any parameters, and there I was thinking this was easy and that I knew what I was doing.
1) So, before this post I thought you can specialize with any one of these (assumed without trying):
1 2 3 4 5 6
|
template<> void DisplayComparison<const char*>(int& a, int& b){}
template<> void DisplayComparison<const char*>(int a, int b){}
template<> void DisplayComparison<const char*>(double a, double b){}
|
...but no, no you can't! They work more specifically than I assumed.
template<> void DisplayComparison<const char*>(const char* const& val1, const char*...
......................................................^^^^^^
You can change the const char* there to any type you want, but you can't change the type for the parameters inside (). They must be the same type as the base template. And you probably need the same number of parameters too (have not played with changing them yet to find out). Otherwise, you really don't need a specialization...just use a separate function on its own!
1 2 3 4
|
template<typename Type>
void DisplayComparison(const Type& val1, const Type& val2)
template<> void DisplayComparison<const char*>(const char* const& val1, const char* const& val2)
|
So, since there are 2 parameters with a const & reference, then the specialization must have two parameters with a const & reference.
Did I get this right????????????????????
2) Thanks for the link, so precious!
1 2 3 4 5 6 7 8 9 10 11 12 13
|
template<class T> class X { /*...*/ }; // (a)
template<class T> void f( T ); // (b)
template<class T> void f( int, T, double ); // (c)
template<class T> void f( T* ); // (d)
template<> void f<int>( int ); // (e)
void f( double ); // (f)
f( b ); // calls (b) with T = bool
f( i, 42, d ); // calls (c) with T = int
f( &i ); // calls (d) with T = int
f( i ); // calls (e)
f( d ); // calls (f)
|
I tried covering his examples to see if I can guess the function called. I was able to guess all of them right, except for one.
f( &i ); // calls (d) with T = int
I was upset at this, because it is a reference to an int and I was sure it would call (e)... f( i );
But no, it calls the template function for a pointer because it provides an address to the pointer. Very sneaky and you have to be extremely careful. Hard for me to catch as a newb.
|
template<class T> void f( T* ); // (d)
|