Specialization of templates for the inner class

consider the following code:

struct X
{
    template <typename T>
    class Y
    {};
 };
template<>
class X::Y<double>{
};

here we specialize in class Y for type double and the code works fine. The problem is that if I change the code to this:

template<typename A>
struct X
{
    template <typename T>
    class Y
    {};
 };
template<typename A>
class X<A>::Y<double>{
};

the compiler will report an error:

'X :: Y': explicit specialization uses partial specialization syntax, use the template <> instead!

Does anyone know how I can specialize class Y in this case?

+4
source share
2 answers

- . , :

#include <iostream>

template<typename A>
struct X
{
    template <typename T, T* =nullptr>
    class Y{};
 };

template<typename A>
template<double *Ptr>
class X<A>::Y<double, Ptr> {
public:
    static constexpr int value = 1;
};

int main() {
    std::cout << X<int>::Y<double>::value << std::endl;
}

[live demo]

+2

, . . .

: , T, A X:

namespace impl
{
    template <typename A, typename T>
    struct Y { };
}

template<typename A>
struct X
{
    template <typename T>
    using Y = impl::Y<A, T>;
};

, , , :

template <>
template <>
class X<int>::Y<double>
{
    // ...
};

wandbox

+3

Source: https://habr.com/ru/post/1660815/


All Articles