Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ function template specialisation

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?

like image 947
ScaryAardvark Avatar asked Oct 12 '11 10:10

ScaryAardvark


People also ask

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

Can you partially specialize a C++ function template?

You can choose to specialize only some of the parameters of a class template. This is known as partial specialization. Note that function templates cannot be partially specialized; use overloading to achieve the same effect.

Is function template overloading possible?

A template function can be overloaded either by a non-template function or using an ordinary function template.

Is class template and function template the same?

The class template in c++ is like function templates. They are known as generic templates. They define a family of classes in C++. Here Type is a placeholder type name, which will be specified when a class instantiated.


3 Answers

If you change the declaration:

template<> void X::func< char * >( const char * & v )

to:

template<> void X::func< char * >( char * const & v )

This would work perfectly fine. Why does it happen? Because while const sometype is perfectly acceptable, it's only an alternative notation for sometype const. So, your const modifier is not applied to the basic type (char), but to the pointer, making "constant pointer to non-constant char" a valid type. If that's not what you want, you'll have to correct your basic template.

Finally, here's some interesting reading for you about why overloading templates is generally better than specializing them.

like image 198
Septagram Avatar answered Sep 30 '22 00:09

Septagram


The reason you face this error is because you write const before the type. Although this is common practise, it is not conducive to understanding how const/volatile-qualifiers (cv-qualifier) work.

In this case const T when T is char* doesn't mean const char*. It rather means char* const because T is char* and no matter which side of T you put const it behaves as if const is on the right of T, 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 const or volatile on the right of the type. For example, it makes it straightforward to mentally expand T const when T is char* to char* const.

This is the reason in boost sources you see cv-qualifiers after the type, not before.

like image 27
Maxim Egorushkin Avatar answered Sep 30 '22 01:09

Maxim Egorushkin


Move const before &

template<> 
void X::func< char * >( char * const & v ) 
{ 
} 
like image 39
Alexey Malistov Avatar answered Sep 30 '22 01:09

Alexey Malistov