Nested class template specialization

nikitablack

A class:

template<typename C, typename T>
class A
{
    template <typename U>
    class Nested{};

    Nested<T> n;
};

And I want to specialize Nested. Here what I tried:

template<typename C, typename T>
class A
{
    template <typename U>
    class Nested{};

    template <>
    class Nested<int>{}; // by my logic this should work by I have a compilation error "explicit specialization in non-namespace scope 'class A<C, T>'"

    Nested<T> n;
};

My next attempt:

template<typename C, typename T>
class A
{
    template <typename U>
    class Nested{};

    Nested<T> n;
};

template<>
A<>::Nested<int>{}; // What is the correct syntax to do it here? Now I have an error "wrong number of template arguments (0, should be 2)"

Here on stackoverflow I found a solution:

template<typename C, typename T>
class A
{
    template <typename U, bool Dummy = true>
    class Nested{}; // why need of this Dummy??

    template <bool Dummy>
    class Nested<int, Dummy>{}; // why need to provide an argument??

    Nested<T> n;
};

It perfectly works, but I can't understand how. Why to provide a dummy template argument? Why can't I use raw specialization template<> class Nested<int, true>{} or template<> class Nested<int>{}?

ForEveR

It's forbidden to create explicit specialization in class-scope:

An explicit specialization shall be declared in a namespace enclosing the specialized template.

But it's not forbidden to create partial specialization:

A class template partial specialization may be declared or redeclared in any namespace scope in which its definition may be defined (14.5.1 and 14.5.2).

this

template <bool Dummy>
class Nested<int, Dummy>{}; // why need to provide an argument??

is partial specialization and it's allowed to create such specialization in class-scope. You also cannot fully specialize nested class, in not-specialized outer class. You can do this:

template<>
template<>
class A<int, double>::Nested<int>
{
};

but you cannot do

template<typename C, typename T>
template<>
class A<C, T>::Nested<int>
{
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Specialization of inherited nested template class

Nested class template specialization issue

Partial specialization of nested template template class

specialization of template template paremeter of template class for nested template class

class template specialization with template

Partial specialization of nested class on enum defined inside a template class

Template class specialization with template class

Template : class specialization

Check if class is a template specialization?

Class template member specialization

Class template method specialization

namespace specialization in template class

Check if class is a template specialization

"expected a '>'" in class template specialization?

Partial specialization of a nested class

error: nested name specifier for declaration does not refer into a class, class template or class template partial specialization

Template function specialization for template class

Template method of template class specialization

Template class template constructor specialization

Template member specialization in template class

template class specialization at template constructor

specialization of class member of a template class

Does the use a simple-template-id in a nested-name-specifier unambiguously mean a class template specialization?

Template specialization on a non template method in a template class

Class template specialization with template template parameters

class specialization, without the template arguments of the class that is used as template argument for specialization

Class template specialization within template class

Template class specialization and friend classes

Class Template specialization for multiple types