Nested class template specialization

对着背影说爱祢 提交于 2019-12-12 10:28:52

问题


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


回答1:


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



回答2:


I managed to get this to work, by defining all the contents of the specialized template class within the outer class. So all functions are completely defined withing the class definition. No external function definitions, as that did not seem to complile. I.e.

template <typename T, size_t N>
class A
{
private:
    template <size_t M>
    class B
    {
        ...
    };

    template <>
    class B<2>
    {
        ...
    };
    ... etc
};

It worked on MS2015 at least. Code is running just fine.



来源:https://stackoverflow.com/questions/33099306/nested-class-template-specialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!