Given this code:
class X
{
public:
template< typename T >
void func( const T & v );
};
template<>
void X::func< int >( const int & v )
{
}
template<>
void X::func< char * >( const char * & v ) // 16
{
}
When I compile it I get the following error.
test.cpp:16: error: template-id 'func<char*>' for 'void X::func(const char*&)' does not match any template declaration
Can anyone shed any light on this?
The reason you face this error is because you write
constbefore the type. Although this is common practise, it is not conducive to understanding how const/volatile-qualifiers (cv-qualifier) work.In this case
const TwhenTischar*doesn’t meanconst char*. It rather meanschar* constbecauseTischar*and no matter which side ofTyou putconstit behaves as ifconstis on the right ofT, that is, the pointer itself that is going to be const not the type pointed too.It is easy to avoid this type of confusion if you make it a rule to always put
constorvolatileon the right of the type. For example, it makes it straightforward to mentally expandT constwhenTischar*tochar* const.This is the reason in boost sources you see cv-qualifiers after the type, not before.