Zej*_*ejj 7 c++ template-argument-deduction c++17
我正在研究一个可变参数类模板,但我不能在没有指定模板参数的情况下使用新表达式(我不想这样做).我将问题减少到以下代码示例:
template <typename T>
struct Foo
{
Foo(T p)
: m(p)
{}
T m;
};
template <typename T1, typename T2>
struct Bar
{
Bar(T1 p1, T2 p2)
: m1(p1), m2(p2)
{}
T1 m1;
T2 m2;
};
int main()
{
double p = 0.;
auto stackFoo = Foo(p); // OK
auto heapFoo = new Foo(p); // OK
auto stackBar = Bar(p, p); // OK
auto heapBar = new Bar(p, p); // error: class template argument deduction failed
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我从cppreference的理解,编译器应该能够在上面的每种情况下推导出模板参数.我无法弄清楚为什么也没有错误heapFoo.
我在这里错过了一些东西吗?
我在Xubuntu 17.10上使用gcc 7.2.0,带-std = c ++ 17标志.
Barry 提交的标题为“类模板参数推导在 new-expression 中失败”的错误85883已在 GCC 9 中修复。
该错误不会出现在 GCC Trunk ( DEMO ) 中。
作为 GCC 7.2 的解决方法,您可以使用如下所示的值初始化形式。(演示):
auto heapBar = new Bar{p, p};
Run Code Online (Sandbox Code Playgroud)