Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested class template specialization

Tags:

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>{}?

like image 704
nikitablack Avatar asked Oct 13 '15 09:10

nikitablack


People also ask

Is specialization of template 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.

Can class template be nested?

Member templates that are classes are referred to as nested class templates. Member templates that are functions are discussed in Member Function Templates. Nested class templates are declared as class templates inside the scope of the outer class. They can be defined inside or outside of the enclosing class.

What are the advantages of nested class in C++?

Nested classes are just like regular classes, but: they have additional access restriction (as all definitions inside a class definition do), they don't pollute the given namespace, e.g. global namespace.


1 Answers

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> { }; 
like image 159
ForEveR Avatar answered Sep 21 '22 18:09

ForEveR