新的表达模板参数推导失败

问题描述 投票:7回答:1

我工作的一个可变参数类模板,但有一个新的表达方式,而无需指定模板参数(我不想)我不能使用它。我降低了问题以下代码示例:

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没有错误了。

所以我失去了一些东西在这里?

我使用的Xubuntu 17.10与-std = C ++ 17标志的gcc 7.2.0。

c++ c++17 template-deduction
1个回答
3
投票

该缺陷85883标题:巴里提起"class template argument deduction fails in new-expression"已修复了GCC 9。

该错误不会出现在GCC干线(DEMO)。

至于GCC 7.2解决方法,您可以使用值初始化形式像下面。 (DEMO):

auto heapBar = new Bar{p, p};
© www.soinside.com 2019 - 2024. All rights reserved.