【发布时间】:2018-05-22 23:59:26
【问题描述】:
我正在处理可变参数类模板,但如果不指定模板参数(我不想),我就无法将它与新表达式一起使用。我将问题简化为以下代码示例:
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;
}
根据我对cppreference 的了解,编译器应该能够在上述每种情况下推断出模板参数。我不明白为什么heapFoo 也没有错误。
那么我在这里错过了什么吗?
我在带有 -std=c++17 标志的 Xubuntu 17.10 上使用 gcc 7.2.0。
【问题讨论】:
-
编译器错误。找不到骗子,提交85883。
标签: c++ c++17 template-argument-deduction