函数参数太多 std::make_shared<vector>

too many arguments to function 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{}, 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

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

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

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

这种情况在 [temp.deduct.call/1] 中的 C++ 标准中有描述:

Template argument deduction is done by comparing each function template parameter type (call it P) that contains template-parameters that participate in template argument deduction with the type of the corresponding argument of the call (call it A) as described below. If removing references and cv-qualifiers from P gives std::initializer_list<P′> or P′[N] for some P′ and N and the argument is a non-empty initializer list ([dcl.init.list]), then deduction is performed instead for each element of the initializer list independently, taking P′ as separate function template parameter types P′i and the ith initializer element as the corresponding argument. In the P′[N] case, if N is a non-type template parameter, N is deduced from the length of the initializer list. Otherwise, an initializer list argument causes the parameter to be considered a non-deduced context ([temp.deduct.type]).

在你的情况下,最后一句话适用。有趣的是,错误消息还说了 GCC 的其他内容,这很奇怪。使用 Clang,错误消息很清楚:

error: no matching function for call to 'f'

note: candidate template ignored: substitution failure: deduced incomplete pack <(no value)> for template parameter 'Ts'