【问题标题】:Template argument deduction failure with new expression新表达式的模板参数推导失败
【发布时间】: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


【解决方案1】:

Barry 提交的标题为:"class template argument deduction fails in new-expression" 的错误 85883 已在 GCC 9 中得到修复。

错误未出现在 GCC 中继 (DEMO) 中。

作为 GCC 7.2 的一种解决方法,您可以使用如下所示的值初始化形式。 (DEMO):

auto heapBar = new Bar{p, p};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2012-10-26
    • 2012-12-06
    相关资源
    最近更新 更多