CMake/Tests/CompileFeatures/cxx_variadic_templates.cpp
Stephen Kelly 1f19ac4da5 Features: Adjust cxx_variadic_templates unit test for GNU < 4.7.
The unit test for this fails with GNU 4.6:

 Building CXX object CMakeFiles/test_cxx_variadic_templates.dir/cxx_variadic_templates.cpp.o
   CompileFeatures/cxx_variadic_templates.cpp: In static member function ‘static int Interface<I, Is>::accumulate()’:
   CompileFeatures/cxx_variadic_templates.cpp:18:31: sorry, unimplemented: cannot expand ‘Is ...’ into a fixed-length argument list
 CMakeFiles/test_cxx_variadic_templates.dir/build.make:54: recipe for target 'CMakeFiles/test_cxx_variadic_templates.dir/cxx_variadic_templates.cpp.o' failed

The workaround is to use a specialization:

 http://stackoverflow.com/questions/1989552
 http://stackoverflow.com/questions/11297376
2015-01-15 22:13:22 +01:00

66 lines
1.3 KiB
C++

template<int... Is>
struct Interface;
template<int I>
struct Interface<I>
{
static int accumulate()
{
return I;
}
};
template<int I, int... Is>
struct Interface<I, Is...>
{
static int accumulate()
{
return I + Interface<Is...>::accumulate();
}
};
// Note: split this into a separate test if a
// cxx_variadic_template_template_parameters feature is added.
template<typename T>
struct eval {
enum {
Matched = 0
};
};
template<template<typename...> class T, typename... U>
struct eval<T<U...> > {
enum {
Matched = 1
};
};
template<typename...>
struct A {
};
template<typename T>
struct B {
};
template<typename T, typename U>
struct C {
};
template<typename T, typename U, typename...>
struct D {
};
// Note: This test assumes that a compiler supporting this feature
// supports static_assert. Add a workaround if that does not hold.
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");