为什么我不能用list-initialization初始化std :: vector

tem*_*boy 6 c++ c++11

为什么这不起作用?

#include <vector>

struct A {
   template <typename T> void f(const std::vector<T> &) {}
};

int main() {

   A a;

   a.f({ 1, 2, 3 });

}
Run Code Online (Sandbox Code Playgroud)

Die*_*ühl 13

可以初始化一个std::vector<T>与列表初始化.但是,您不能使用参数列表中的a 推导出模板参数,并将函数传递给非函数.例如,这有效:Tstd::vector<T>std::vector<T>

#include <vector>

template <typename T>
struct A {
   void f(const std::vector<T> &) {}
};

int main() {

    A<int> a;

   a.f({ 1, 2, 3 });

}
Run Code Online (Sandbox Code Playgroud)