Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialiation of template class that is member of template class

Tags:

c++

templates

Is it possible to have a specialized template class which is a member of a template class(that is not specialized per se)?

With a non-template parent class this works:

class owner
{
    template<int num>
    struct s
    {
        int ret() { return num; }
    };
};

template<>
struct owner::s<0>
{
    int ret() { return 0; }
};

But when making owner a template class it does not:

template<typename some>
class owner
{
    template<int num>
    struct s
    {
        int ret() { return num; }
    };
};

template<typename some>
struct owner<some>::s<0>
{
    int ret() { return 0; }
};

Searching shows that this is not possible(?) for functions, but how about classes/structs? Specializing a templated member of a template class

like image 506
Aart Stuurman Avatar asked Mar 22 '19 09:03

Aart Stuurman


People also ask

What is called specialization in template?

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.

Can a template method be a member of a non template class?

It's not the method that is templated, it's the class. You can have a templated method in a non-templated class, a non-templated method in a templated class (your case) and a templated method in a templated class, and of course a non-templated method in a non-templated class. Save this answer.

How would you define member function of template class?

Member functions of class templates (C++ only)You may define a template member function outside of its class template definition. The overloaded addition operator has been defined outside of class X . The statement a + 'z' is equivalent to a. operator+('z') .

Can a template class inherit from another template class?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.


1 Answers

No, that is not possible. A member class template can only be specialised if all of its enclosing class templates are also specialised. Quoting C++2x (N4713) [temp.expl.spec] 17.8.3/17:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

(Emphasis mine)

In some cases, you could get around this by making the nested name s an alias to a namespace-scope helper which can be partially specialised. Something like this:

template <class some, int num>
struct s_helper
{
  int ret() { return num; }
};

template<typename some>
class owner
{
    template<int num>
    using s = s_helper<some, num>;
};

template<typename some>
struct s_helper<some, 0>
{
    int ret() { return 0; }
};

To reduce the exposure of s_helper, it can of course be hidden in a suitably named internal namespace (such as detail).

like image 164
Angew is no longer proud of SO Avatar answered Oct 03 '22 03:10

Angew is no longer proud of SO