Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial specialization of a method in a templated class

Tags:

Given:

struct A {     virtual bool what() = 0; };  template<typename T, typename Q> struct B : public A {     virtual bool what(); }; 

I want to partially specialize what like:

template<typename T, typename Q> bool B<T, Q>::what() {     return true; }  template<typename Q> bool B<float, Q>::what() {     return false; } 

But it appears that this isn't possible (is it in C++11?) so I tried SFINAE:

template<typename T> typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what() {     return true; }  template<typename T> typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what() {     return false; } 

This also doesn't work, I have no idea why though, does anyone? So I found this thread and ended up with:

template<typename T, typename Q> struct B : public A {     virtual bool what()     {         return whatimpl(std::is_same<T, float>());     }      bool whatimpl(std::false_type)     {         return false;     }      bool whatimpl(std::true_type)     {         return true;     } }; 

This final solution works, but why doesn't the enable_if technique work? I'm also very open to suggestions of a cleaner answer that I haven't encountered yet.

I simplified my examples as much as possible - in my real use case what() isn't called what and actually does a fair bit of work, and I'll want to 'specialize' on a user defined type, not float.

like image 652
David Avatar asked Apr 23 '12 16:04

David


People also ask

What is explicit Specialisation?

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

What is meant by template specialization in C++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the other name of full specialization?

Explanation: explicit specialization is another name of full specialization.

Can a non templated class have a templated member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.


1 Answers

Partial specialization is explicitly permitted by the standard only for class templates (see 14.5.5 Class template partial specializations)

For members of class template only explicit specialization is allowed.

14.7 (3) says:

An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. An explicit specialization declaration is introduced by template<>.

So any definition starting with

template<typename T>   

is not an allowed syntax for member of class template specialization.

[edit]

As to SFINAE attempt, it failed because actually there are neither overloads nor specializations here (SFINAE works while defining a set of candidate functions for overload resolution or while choosing proper specialization). what() is declared as a single method of class template and should have a single definition, and this definition should have a form:

template<typename T, typename Q>  B<T,Q>:: bool what(){...} 

or may be also explicitly specialized for particular instantiation of class B:

template<>  B<SomeParticularTypeT,SomeParticularTypeTypeQ>:: bool what(){...} 

Any other forms are syntacticaly invalid, so SFINAE can't help.

like image 135
user396672 Avatar answered Sep 28 '22 22:09

user396672