Michel Michel -4 years ago 70
C++ Question

C++ template specializations failing with non type parameter pack

My question might be related to this one, but I think I don't have a "partially specialized non-type argument expression" here, or don't understand the relation.

The following code produce an internal error with MSVC14 compiler (CPP11):

template<typename T, T... Elmts>
struct NonTyped
{

};

template<typename T>
struct is_NonTyped_of_type
{
template<typename TL>
static constexpr bool check = false;

template<T... Elmts>
static constexpr bool check<NonTyped<T, Elmts...>> = true;
};

cout << is_NonTyped_of_type<int>::check<NonTyped<int, 5>> << endl;


Using only one non-type parameter instead of a non-type parameter pack would work as expected, but this fails.

Is this forbidden or undefined by the standard ? What rule does it break ?

Any workaround ?

Thank you very much!

Answer Source

Clang accepts your code, while GCC did not. The partial specialization of your variable template should be okay as it is. You can always go back to the tried and tested way of using a regular class template to do the actual calculation.

template<typename T>
class is_NonTyped_of_type
{
    template<typename TL>
    struct check_impl : std::false_type {};

    template<T... Elmts>
    struct check_impl<NonTyped<T, Elmts...>> : std::true_type {};

private:
    template<typename TL>
    static constexpr bool check = check_impl<TL>::value;
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download