函数std :: make_shared <vector>的参数过多

我缺少std::make_shared的内容。它不能解析std::initializer_list的类型,还是我做错了其他事情?

#include <vector>
#include <memory>

class A {};

int main()
{
    A a;
    std::vector<A> veca{A(),A{},a}; // this works ofc
    std::vector<A> vecb({A(),a}); // this too

    std::make_shared<std::vector<A>>(vecb); // and this,ofc
    std::make_shared<std::vector<A>>({a}); // what's wrong here?
    return 0;
}

错误:

main.cpp:21:41: error: too many arguments to function ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = std::vector; _Args = {}]’
     std::make_shared<std::vector<A>>({a});
                                         ^
In file included from /usr/include/c++/6/memory:82:0,from main.cpp:10:
/usr/include/c++/6/bits/shared_ptr.h:632:5: note: declared here
     make_shared(_Args&&... __args)
     ^~~~~~~~~~~

实时示例:https://onlinegdb.com/r1DlHquDL

iloveuvm 回答:函数std :: make_shared <vector>的参数过多

请考虑以下问题的最小示例:

template <typename... Ts>
void f(Ts&&...);  // replacement for std::make_shared

int main()
{  
  f({1});
}

[temp.deduct.call/1]的C ++标准中对此情况进行了说明:

模板参数推导是通过将包含参与模板参数推导的模板参数的每个函数模板参数类型(称为P)与调用的相应参数的类型(称为{{ 1}}),如下所述。如果从A中删除引用和简历限定符,则给出 对于某些Pstd::initializer_list<P′>的{​​{1}}或P′[N],并且该参数是非空的初始值设定项列表([dcl.init.list]),则对初始化器列表中的每个元素都是独立的,将P′作为单独的功能模板参数类型N,将第P′个初始化器元素作为相应的参数。在P′i的情况下,如果i是非类型模板参数,则从初始化器列表的长度推导P′[N]否则,初始化程序列表参数会导致该参数被视为非推论上下文([temp.deduct.type])。

以您的情况为准。有趣的是,错误消息还显示了GCC的其他信息,这很奇怪。使用Clang,错误消息很明显:

N

N

本文链接:https://www.f2er.com/2502237.html

大家都在问