gpt4 book ai didi

c++ - 为什么不能推导出这个模板参数?

转载 作者:行者123 更新时间:2023-12-05 09:05:06 24 4
gpt4 key购买 nike

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
}

在我的 Visual Studio 编译器中,它以红色突出显示表示错误的区域,但可以编译。在 C++17 下的 onlineGDB 上编译失败并出现上述错误。这是允许的吗?有什么不应该的理由吗?

编辑:我意识到使用是多么愚蠢,因为 Foo() 不是一种类型,但 '= Foo' 和 '= decltype(Foo())' 都不起作用。

最佳答案

问题是 Foo不是类型,它是类型的模板

您需要在此处指定一个实际类型,它需要模板尖括号,Foo<>这是类型 Foo<double, 2, 3> :

typename FooType = Foo<>

关于c++ - 为什么不能推导出这个模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67715611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com