Paolo M
Paolo M

Reputation: 12777

VS2015: Variadic template specialization

This code

#include <iostream>
#include <type_traits>

template<typename Head, typename... Tail>
struct Is_Admitted {
    constexpr static bool value = Is_Admitted<Head>::value && Is_Admitted<Tail...>::value;
};

template<>
template<typename T>
struct Is_Admitted<T> : public std::false_type{};

template<> struct Is_Admitted<int> : public std::true_type{};
template<> struct Is_Admitted<double> : public std::true_type{};

int main()
{
    std::cout << Is_Admitted<int, double>::value << '\n';
    std::cout << Is_Admitted<int, char>::value << '\n';
}

(error descriptions are translated in English by myself as I was not able to set English as compiler language, so they might mismatch with original ones)

error C2910: 'Is_Admitted<T,>': impossible to perform explicit specialization
error C2065: 'value': undeclared identifier
error C2976: 'Is_Admitted': insufficients template arguments
error C2131: constant expression does not return any value

Which compiler is right and which one is wrong? Is that code compliant to either c++11, c++14 or c++17 standard?

And what is the right way to do what I'm trying to do, that is a variadic type function that returns true only if all template type parameters are of some admitted types?

Upvotes: 0

Views: 577

Answers (1)

Barry
Barry

Reputation: 303347

You have an extra template<> here:

template<>   // <===
template<typename T>
struct Is_Admitted<T> : public std::false_type{};

Your code gives me the same error via webcompiler. Simply remove it and it compiles fine. I do not understand how this compiles on either gcc or clang.

Two template declarations are only necessary when you're defining a member template of a class template outside of the class definition, e.g.:

template <typename T>
struct Foo {
    template <typename U>
    void bar();
};

template <typename T>
template <typename U>
void Foo<T>::bar() { ... }

Upvotes: 1

Related Questions