vendredi 20 avril 2018

Definition of C++ template specialization not working when expected

This is a follow up to this question. The answer to that question works as advertised. However, I was struggling to apply it to my actual code. I now have an example that does not work, but I believe matches the previous question's answer.

#include <type_traits>

namespace Internal
{
    template <template<typename...> class TT, typename T>
    struct IsInstantiation : std::false_type {};

    template <template<typename...> class TT, typename... Ts>
    struct IsInstantiation<TT, TT<Ts...>> : std::true_type {};

    template <typename T1, typename = void>
    struct MyClass
    {
        void func(int i);
    };

    template <typename T1>
    struct MyClass<T1, std::enable_if_t<IsInstantiation<MyClass, T1>::value>>
    {
        void func(int i);
    };
}

template <typename T1, typename T2>
inline void Internal::MyClass<T1, T2>::func(int i) {}

template <typename T1>
inline void Internal::MyClass<T1, std::enable_if_t<Internal::IsInstantiation<Internal::MyClass, T1>::value>>::func(int i) {}

int main(int argc, char *argv[])
{
    Internal::MyClass<int> mc1;
    Internal::MyClass<Internal::MyClass<int>> mc2;

    mc1.func(0);
    mc2.func(0);
}

I am compiling this code with VS2015 Update 3. On the second definition of func it gives the following errors:

  • 'void Internal::MyClass<T1,<unnamed-symbol>>::func(int)': function template has already been defined
  • 'Internal::MyClass<T1,<unnamed-symbol>>': too few template arguments

What am I missing that is causing this to fail to compile?

Aucun commentaire:

Enregistrer un commentaire