小编Zej*_*ejj的帖子

使用新表达式的模板参数推断失败

我正在研究一个可变参数类模板,但我不能在没有指定模板参数的情况下使用新表达式(我不想这样做).我将问题减少到以下代码示例:

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)

根据我从 …

c++ template-argument-deduction c++17

7
推荐指数
1
解决办法
133
查看次数

标签 统计

c++ ×1

c++17 ×1

template-argument-deduction ×1