6
为什么不能正常工作?为什么我无法使用列表初始化初始化std :: vector
#include <vector>
struct A {
template <typename T> void f(const std::vector<T> &) {}
};
int main() {
A a;
a.f({ 1, 2, 3 });
}
为什么不能正常工作?为什么我无法使用列表初始化初始化std :: vector
#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>
与列表初始化。但是,您不能在参数列表中使用推导出模板参数T
使用std::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 });
}
因为它无法从裸括号列表初始化程序中派生类型信息。 'a.f({1,2,3});'应该工作。 –
jogojapan