Foo 的参数有默认值,因此在 main() 中我可以执行 Foo(); 创建一个 Foo ,它将具有默认的模板参数。但是我不能在模板参数中使用:
template <typename T = double, int a =2, int b = 3>
struct Foo {};
//cannot deduce template arguments for ‘Foo’ from ()
template <typename FooType = Foo()> // <-- Error
struct TemplatedStructTakingAFooType
{
};
int main()
{
Foo(); // Foo type is deduced to be default values, ie.,
//Foo<double, 2, 3>;
decltype(Foo()); // Compiler knows the type
}
Run Code Online (Sandbox Code Playgroud)
在我的 Visual Studio 编译器中,它以红色突出显示区域,指示错误,但可以编译。在 C++17 下的 onlineGDB 上编译失败并出现上述错误。这是允许的吗?有什么理由不应该吗?
编辑:我意识到使用是多么愚蠢,因为 Foo() 不是一种类型,但 '= Foo' 和 '= decltype(Foo())' 也不起作用。
问题是这Foo不是一个类型,它是一个类型的模板。
您需要在此处指定一个实际类型,它需要模板尖括号,Foo<>即类型Foo<double, 2, 3>:
typename FooType = Foo<>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |