5

If I do partial specialization I got different results from clang and g++.

template < typename T>
class X
{
    public:
        T i;
        X(T _i): i{_i}{}

        operator T(){ return i; }
};  

template < typename T2 >
class Y
{
    public:
        template <typename T>
            static X<T> x_in_y;
};  

template< typename T2> 
template< typename T>
X<T> Y<T2>::x_in_y{200};


template<>
template<>
X<float> Y<int>::x_in_y<float>{100};

template<>
template<>
X<int> Y<int>::x_in_y<int>{101};

template<  >
template< typename T >
X<T> Y<bool>::x_in_y{77};



int main()
{
    std::cout << Y<int>::x_in_y<int> << std::endl;
    std::cout << Y<int>::x_in_y<float> << std::endl;

    std::cout << Y<float>::x_in_y<float> << std::endl;
    std::cout << Y<bool>::x_in_y<float> << std::endl;
}

I compiled with g++ and clang and got different behavior:

[~]$ g++ main.cpp 
[~]$ a.out 
101
100
200
200

[~]$ clang++ main.cpp 
[~]$ a.out 
101
100
200
77

Bonus: Is it possible to specialize the other way around?:

template< typename T2 >
template<  >
X<int> Y<T2>::x_in_y{105};
max66
  • 65,235
  • 10
  • 71
  • 111
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • But here I would say that this is gcc that has a bug. – Oliv Dec 08 '18 at 23:14
  • 1
    I am not a language lawyer, but I filled a bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88417. If my assumptions are wrong, give me a note and will add/change/delete the report accordingly – Klaus Dec 08 '18 at 23:20
  • 1
    If you sprinkle a bit of `constexpr` everywhere gcc complains about a duplicate definition (the 77 one). Probably has something to do with it. https://godbolt.org/z/ZP8iCH – Rakete1111 Dec 08 '18 at 23:27

0 Answers0