Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to prevent template to specialize a pointer?

Tags:

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.

Why not specialize function templates?

The rationale for why specializations don't participate in overloading is simple, once explained, because the surprise factor is exactly the reverse: The standards committee felt it would be surprising that, just because you happened to write a specialization for a particular template, that it would in any way change ...

What is explicit template specialization?

Explicit (full) specializationAllows customizing the template code for a given set of template arguments.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.


I start apologising if I am stupid enough to not find the answer if it is so obvious.

I have seen dozens of pages talking about having specific template specialization for pointer parameters.

I'd like to be able, though, of preventing a template to specialize pointer parameters, but I can't figure out how to do this.

template< class T >
void function( T arg )
{
  //...
}

int main( )
{
    int i = 42;

    function( i );    // Ok
    function( &i );   // Die bastart with a compiler error!
}

Is it possible?

Thanks.