Matching variadic non-type templates
Asked Answered
W

3

22

Let's say I have two structs, Foo and Bar:

template<int...>
struct Foo{};

template<unsigned long...>
struct Bar{};

I want to create a type trait (call it match_class) that returns true if I pass two Foo<...> types or two Bar<...> types, but false if I try to mix them:

int main()
{
    using f1 = Foo<1, 2, 3>;
    using f2 = Foo<1>;
    using b1 = Bar<1, 2, 3>;
    using b2 = Bar<1>;
    static_assert(match_class<f1, f2>::value, "Fail");
    static_assert(match_class<b1, b2>::value, "Fail");
    static_assert(!match_class<f1, b1>::value, "Fail");
}

For C++1z (clang 5.0.0 and gcc 8.0.0) it's sufficient to do this (Demo):

template<class A, class B>
struct match_class : std::false_type{};

template<class T, template<T...> class S, T... U, T... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

But in C++14 I get the following error (same compilers* Demo):

error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct match_class<S<U...>, S<V...>> : std::true_type{};
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: non-deducible template parameter 'T'
template<class T, template<T...> class S, T... U, T... V>

Question: What is a workaround for this in C++14?

Ideally the syntax for testing the type trait should remain the same.

Secondary question: Is the behavior for C++14 correct? (or alternatively is the behavior I see for C++17 unspecified?)

*Note that MSVC 19.00.23506 has the same kind of failure Demo

Winshell answered 30/5, 2017 at 13:21 Comment(4)
It just works using GCC. Interesting.Schist
Even more, it works also with clang 4.0.0. Probably a defect not yet implemented in either GCC or clang.Schist
Side note - in C++17 you can use this: template<template<auto ...> class S, auto... U, auto... V> struct match_class<S<U...>, S<V...>> : std::true_type{};.Schist
@skypjack: Well put. I considered that for the question, but since the use of auto template arguments is not C++14 compatible, I left it out :-)Winshell
C
17

In C++14, you could not deduce T in:

template<class T, template<T...> class S, T... U, T... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

but in C++17, you can. The behavior you see is correct.

In C++14, since you cannot deduce T, you need a way of explicitly providing it. So you might require the class templates themselves to indicate their non-type template parameter type:

template <int...> struct Foo { using type = int; };
template <unsigned long...> struct Bar { using type = unsigned long; };

Or have an external trait for this. And then explicitly write out everything - two class templates match if they have the same non-type template parameter and then also have the same class template, in that order:

template <class... Ts> struct make_void { using type = void; };
template <class... Ts> using void_t = typename make_void<Ts...>::type;

template <class T1, class T2, class A, class B>
struct match_class_impl : std::false_type { };

template <class T, template <T...> class S, T... U, T... V>
struct match_class_impl<T, T, S<U...>, S<V...>> : std::true_type{};

template <class A, class B, class=void>
struct match_class : std::false_type { };

template <class A, class B>
struct match_class<A, B, void_t<typename A::type, typename B::type>>
    : match_class_impl<typename A::type, typename B::type, A, B>
{ };

This is a consequence of adding support for template auto. In C++14, [temp.deduct.type] contained:

A template type argument cannot be deduced from the type of a non-type template-argument. [Example:

template<class T, T i> void f(double a[10][i]);
int v[10][20];
f(v); // error: argument for template-parameter T cannot be deduced

-end example]

But in C++17, it now reads:

When the value of the argument corresponding to a non-type template parameter P that is declared with a dependent type is deduced from an expression, the template parameters in the type of P are deduced from the type of the value. [ Example:

template<long n> struct A { };

template<typename T> struct C;
template<typename T, T n> struct C<A<n>> {
  using Q = T;
};

using R = long;
using R = C<A<2>>::Q;           // OK; T was deduced to long from the
                                // template argument value in the type A<2>

— end example ] The type of N in the type T[N] is std​::​size_­t. [ Example:

template<typename T> struct S;
template<typename T, T n> struct S<int[n]> {
  using Q = T;
};

using V = decltype(sizeof 0);
using V = S<int[42]>::Q;        // OK; T was deduced to std​::​size_­t from the type int[42]

— end example ]

Congruence answered 30/5, 2017 at 14:32 Comment(0)
S
6

Question: What is a workaround for this in C++14?

A possible workaround in C++14 is based on traits.
As a minimal, working example (maybe even stupid, but it helps getting the idea):

#include <type_traits>
#include <utility>

template<int...>
struct Foo{};

template<unsigned long...>
struct Bar{};

template<typename>
struct traits;

template<int... V>
struct traits<Foo<V...>> { using type = Foo<0>; };

template<unsigned long... V>
struct traits<Bar<V...>> { using type = Bar<0>; };

template<typename T, typename U>
constexpr bool match = std::is_same<typename traits<T>::type, typename traits<U>::type>::value;

int main() {
    using f1 = Foo<1, 2, 3>;
    using f2 = Foo<1>;
    using b1 = Bar<1, 2, 3>;
    using b2 = Bar<1>;

    static_assert(match<f1, f2>, "Fail");
    static_assert(match<b1, b2>, "Fail");
    static_assert(!match<f1, b1>, "Fail");
}

As a side note, in C++17 you can simplify things up as it follows:

template<template<auto ...> class S, auto... U, auto... V>
struct match_class<S<U...>, S<V...>> : std::true_type{};

About the reasons that are behind the error, @Barry's answer contains all what you need to understand it (as usual).

Schist answered 30/5, 2017 at 14:43 Comment(3)
Do you think it could be extended for non-numerical non type template parameters e.g. pointers/references?Hyponitrite
@Hyponitrite Well, yes. It's a matter of adding a bunch of different traits to match them.Schist
Oh! I see it now... One could even left the trait type parameter pack empty for that case. Thanks!Hyponitrite
S
1

Here is a general C++14 solution that does not rely on manually specialized type traits or extending Foo and Bar.

A template metafunction that obtains a type representing the class template of its argument type:

namespace detail
{
    // Type representing a class template taking any number of non-type template arguments.
    template <typename T, template <T...> class U>
    struct nontype_template {};
}

// If T is an instantiation of a class template U taking non-type template arguments,
// this has a nested typedef "type" that is a detail::nontype_template representing U.
template <typename T>
struct nontype_template_of {};

// Partial specializations for all of the builtin integral types.
template <template <bool...> class T, bool... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<bool, T>; };
template <template <char...> class T, char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<char, T>; };
template <template <signed char...> class T, signed char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<signed char, T>; };
template <template <unsigned char...> class T, unsigned char... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned char, T>; };
template <template <short...> class T, short... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<short, T>; };
template <template <unsigned short...> class T, unsigned short... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned short, T>; };
template <template <int...> class T, int... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<int, T>; };
template <template <unsigned int...> class T, unsigned int... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned int, T>; };
template <template <long...> class T, long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<long, T>; };
template <template <unsigned long...> class T, unsigned long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned long, T>; };
template <template <long long...> class T, long long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<long long, T>; };
template <template <unsigned long long...> class T, unsigned long long... Vs>
struct nontype_template_of<T<Vs...>> { using type = detail::nontype_template<unsigned long long, T>; };

An alias template for ease of use:

// Alias template for nontype_template_of.
template <typename T>
using nontype_template_of_t = typename nontype_template_of<T>::type;

Then you can implement your match_class trait just like this:

template <class A, class B>
struct match_class : std::is_same<nontype_template_of_t<A>, nontype_template_of_t<B>> {};

DEMO

Seringapatam answered 30/5, 2017 at 17:38 Comment(4)
Man, I really did not want to exhaustively specify all integral types. Hats off to you for doing so.Winshell
We would also need to provide a nontype_template_of specialization for the types in <cstdint>Winshell
Those are just typedefs to the builtin types, so such specializations would be redefinitions. I did miss out specializations for the wide character types, though, you could add those if you need them.Seringapatam
Thanks. I suppose another compelling reason to switch to C++17 is that you don't have to worry about whether you remembered to get all them or not.Winshell

© 2022 - 2024 — McMap. All rights reserved.