Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template class specialization: why do common methods need to be re-implemented

In the sample:

#include <iostream>

using namespace std;

class B
{
public:
    virtual void pvf() = 0;
};

template <class T>
class D : public B
{
public:
    D(){}

    virtual void pvf() {}

private:
    string data;
};

template <>
class D<bool> : public B
{
public:
    D();

    virtual void pvf(){ cout << "bool type" << endl; }
};

int main()
{
    D<int> d1;
    D<bool> d2;
}

I get the following error:

test.cpp:(.text+0x1c): undefined reference to `D<bool>::D()'

Note that the reason I don't just specialize the D() by itself is I want to eliminate the need for string D<T>::data in the D<bool> case.

Why do I need to re-implement D() in D<bool>? Seems like there should be a way for me to tell the compiler to use the version from D<T>.

Is there any way to do a simple specialization like this without having to re-implement methods?

like image 697
Jaime Avatar asked Jan 09 '12 14:01

Jaime


People also ask

What is the difference between partial specialization and template specialization?

Templates can have more than one parameter type. Some older compilers allow one only to specialize either all or none of the template's parameters. Compilers that support partial specialization allow the programmer to specialize some parameters while leaving the others generic.

What is a template class what is the advantage of using a template class?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.

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 are template functions why they are used in C++? Explain briefly?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.


1 Answers

Each specialisation of a class template gives a different class - they do not share any members with each other. Since you've explicitly specialised the entire class, you don't get any of the members from the template, and must implement them all.

You can explicitly specialise individual members, rather than the entire class:

template <> void D<bool>::pvf(){ cout << "bool type" << endl; }

Then D<bool> will still contain all the members of the class template that you haven't explicitly specialised, including the default constructor.

like image 123
Mike Seymour Avatar answered Oct 31 '22 18:10

Mike Seymour