non*_*ion 7 c++ templates language-lawyer variadic-templates c++11
为什么可变参数模板包中不允许特定类型?
template< typename T >
class Foo
{
public:
template< typename... Values >
void bar( Values... values )
{
}
template< T... values > <-- syntax error
void bar( T... values )
{
}
template< int... values > <-- syntax error
void bar( int... values )
{
}
};
Run Code Online (Sandbox Code Playgroud)
什么在的理由不使这个?
是否有这方面的建议?
注意:替代方案是
std::initializer_list< T >没有缩小类型和{ }-brace语法实际上,你只是允许使用它是错误的.T...并且int...是非类型参数包,它们的元素是值,因此您不能将它们用作类型说明符(并且您不能从函数调用中推断它们).
正确用法的一个例子:
template<int... Is>
struct IntPack {};
IntPack<1,2,3> p;
Run Code Online (Sandbox Code Playgroud)
要么
template< typename T >
struct Foo
{
template< T... Ts>
void bar()
{
}
};
int main()
{
Foo<int> f;
f.bar<1,2,3>();
}
Run Code Online (Sandbox Code Playgroud)
另一个例子是std::integer_sequence.
| 归档时间: |
|
| 查看次数: |
2073 次 |
| 最近记录: |