我用的C++ 0x可变参数模板实验时我偶然发现了这个问题:是否可以“存储”模板参数包而不扩展它?
template < typename ...Args >
struct identities
{
typedef Args type; //compile error: "parameter packs not expanded with '...'
};
//The following code just shows an example of potential use, but has no relation
//with what I am actually trying to achieve.
template < typename T >
struct convert_in_tuple
{
typedef std::tuple< typename T::type... > type;
};
typedef convert_in_tuple< identities< int, float > >::type int_float_tuple;
GCC 4.5.0给我一个错误,当我尝试的typedef模板参数包。
基本上,我想将参数包“存储”在typedef中,而不用解包它。可能吗?如果不是,有什么理由不允许这样做?
我在GCC 4.5上试过了你的代码,你只需要在`class T`中改变`typename T`并将`convert_in_tuple`参数改为template template模板参数:`template class> class T> struct convert_in_tuple {...}`(!)。 – 2011-01-14 14:51:14
@Luc:编辑为模板模板参数。用`class`代替`typename`会让人有点怀疑,因为草案说“**模板参数*”中**类**和**`模板**之间没有语义上的区别。“,你试试这个新的代码? – 2011-01-14 14:53:22
我无法在标准中找到它,但我想我记得对于模板模板参数,您需要使用`class`而不是`typename`(因为模板类型不可避免地是类而不是任何类型)。 – 2011-01-14 15:01:18