Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template class member function only specialization

I am reading the Complete Guide on Templates and it says the following:

Where it is talking about class template specialization.

Although it is possible to specialize a single member function of a class template, once you have done so, you can no longer specialize the whole class template instance that the specialized member belongs to.

I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a specialization with only one member function and then another with all member functions?

Can someone please clarify?

like image 684
Tony The Lion Avatar asked May 10 '11 12:05

Tony The Lion


People also ask

Can a member function be a template?

Member functions can be function templates in several contexts. All functions of class templates are generic but are not referred to as member templates or member function templates. If these member functions take their own template arguments, they are considered to be member function templates.

What is function template specialization?

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. A primary template is the template that is being specialized.

What is explicit template specialization?

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

What does template <> mean in C++?

What Does Template Mean? A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.


2 Answers

I think it is referring to the following case:

template <typename T> struct base {    void foo() { std::cout << "generic" << std::endl; }    void bar() { std::cout << "bar" << std::endl; } }; template <> void base<int>::foo() // specialize only one member {     std::cout << "int" << std::endl;  } int main() {    base<int> i;    i.foo();         // int    i.bar();         // bar } 

Once that is done, you cannot specialize the full template to be any other thing, so

template <> struct base<int> {};  // error 
like image 112
David Rodríguez - dribeas Avatar answered Oct 10 '22 22:10

David Rodríguez - dribeas


I think what is meant is that you can either:

  • specialize the whole class and all members (data and functions, static or not, virtual or not) have to be declared and defined even if they are the same as for the non specialized version,

  • specialize some function members, but then you can't specialize the whole class (i.e. all members are declared in the same way as for the non specialized case, you just provide the implementation for some function members).

like image 25
AProgrammer Avatar answered Oct 10 '22 21:10

AProgrammer