cmake/Tests/CompileFeatures/cxx_variadic_templates.cpp

73 lines
1.4 KiB
C++
Raw Normal View History

2015-04-27 22:25:09 +02:00
#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 407)
2018-08-09 18:06:22 +02:00
# define OLD_GNU
2015-04-27 22:25:09 +02:00
#endif
#ifdef OLD_GNU
2016-07-09 11:21:54 +02:00
template <int... Is>
2015-04-27 22:25:09 +02:00
struct Interface;
#endif
2016-07-09 11:21:54 +02:00
template <int I, int... Is>
2015-04-27 22:25:09 +02:00
struct Interface
#ifdef OLD_GNU
2016-07-09 11:21:54 +02:00
<I, Is...>
2015-04-27 22:25:09 +02:00
#endif
{
2016-07-09 11:21:54 +02:00
static int accumulate() { return I + Interface<Is...>::accumulate(); }
2015-04-27 22:25:09 +02:00
};
2016-07-09 11:21:54 +02:00
template <int I>
2015-04-27 22:25:09 +02:00
struct Interface<I>
{
2016-07-09 11:21:54 +02:00
static int accumulate() { return I; }
2015-04-27 22:25:09 +02:00
};
// Note: split this into a separate test if a
// cxx_variadic_template_template_parameters feature is added.
2016-07-09 11:21:54 +02:00
template <typename T>
struct eval
{
enum
{
2015-04-27 22:25:09 +02:00
Matched = 0
};
};
2016-07-09 11:21:54 +02:00
template <template <typename...> class T, typename... U>
2018-01-26 17:06:56 +01:00
struct eval<T<U...>>
2016-07-09 11:21:54 +02:00
{
enum
{
2015-04-27 22:25:09 +02:00
Matched = 1
};
};
2016-07-09 11:21:54 +02:00
template <typename...>
struct A
{
2015-04-27 22:25:09 +02:00
};
2016-07-09 11:21:54 +02:00
template <typename T>
struct B
{
2015-04-27 22:25:09 +02:00
};
2016-07-09 11:21:54 +02:00
template <typename T, typename U>
struct C
{
2015-04-27 22:25:09 +02:00
};
2016-07-09 11:21:54 +02:00
template <typename T, typename U, typename...>
struct D
{
2015-04-27 22:25:09 +02:00
};
// Note: This test assumes that a compiler supporting this feature
// supports static_assert. Add a workaround if that does not hold.
2018-01-26 17:06:56 +01:00
static_assert(eval<A<>>::Matched, "A Matches");
static_assert(eval<A<int>>::Matched, "A Matches");
static_assert(eval<A<int, char>>::Matched, "A Matches");
static_assert(eval<B<int>>::Matched, "B Matches");
static_assert(eval<C<int, char>>::Matched, "C Matches");
static_assert(eval<D<int, char>>::Matched, "D Matches");
static_assert(eval<D<int, char, bool>>::Matched, "D Matches");
static_assert(eval<D<int, char, bool, double>>::Matched, "D Matches");