Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么这不起作用?
#include <vector> struct A { template <typename T> void f(const std::vector<T> &) {} }; int main() { A a; a.f({ 1, 2, 3 }); }
您可以std::vector<T>使用列表初始化来初始化。但是,您不能使用参数列表中的 a来推断模板参数,并将非 a 的东西传递给函数。例如,这有效:Tstd::vector<T>std::vector<T>
std::vector<T>
T
#include <vector> template <typename T> struct A { void f(const std::vector<T> &) {} }; int main() { A<int> a; a.f({ 1, 2, 3 }); }