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