我想问一下以下代码是否有效。
我想知道在一个表达式中多次扩展参数包的可能性。
#include <iostream>
#include <tuple>
class ExpandWithConstructor
{
public:
template <typename ... T>
ExpandWithConstructor( T... args) { }
};
template <typename T>
int PrintArgs( T arg )
{
std::cout << arg << ", ";
return 0;
}
template <typename Head, typename ... T>
class DebugPrinter: public DebugPrinter<T...>
{
public:
DebugPrinter() { }
template< typename ...Y>
DebugPrinter( Y ... rest )
{
std::cout << "Construction of: " << __PRETTY_FUNCTION__ << " Values: " ;
ExpandWithConstructor{PrintArgs( rest)...};
std::cout << std::endl;
}
};
template <typename Head>
class DebugPrinter< Head >
{
public:
};
template <typename ... T>
class TypeContainer: public std::tuple<T...>
{
public:
TypeContainer(T... args):std::tuple<T...>(args...){};
};
template <typename... T1> class CheckVariadic;
template <typename... T1, typename ...T2>
class CheckVariadic< TypeContainer<T1...>, TypeContainer<T2...>> :
public DebugPrinter< T1, T2, T1...>...
{
public:
CheckVariadic( T1... args1, T2... args2, T1... args3): DebugPrinter< T1, T2, T1...>(args1, args2..., args1)... {}
};
int main()
{
CheckVariadic< TypeContainer<int,float>, TypeContainer<char, void*>> checkVariadic1{ 1,2.2,'c',(void*)0xddddd,5,6.6,};
}
如您所见,代码使用:DebugPrinter< T1, T2, T1...>...
如果 T1 与“int,float”一起给出,而 T2 是“char,void*”,则扩展为
DebugPrinter< T1, T2, int, float>...
扩展到
DebugPrinter< int, char, int, float>
DebugPrinter< float, void*, int, float>
相同的扩展适用于:
DebugPrinter< T1, T2, T1...>(args1, args2..., args1)...
代码用clang3.3编译,但不能用gcc4.8.1编译,所以我想问一下代码是否有效。
更新:gcc 7.2 仍然没有编译代码。