【发布时间】:2020-04-28 18:25:51
【问题描述】:
我是模板新手,我真的不明白为什么这不起作用。我希望用这些值构造向量。
main.cpp
template <typename ...T>
void int_printf(T ...args)
{
std::vector<T> vec = {args...};
for(auto& v:vec)
{
std::cout << v << std::endl;
}
}
int main()
{
int_printf(1,2,3,4);
return 0;
}
预期结果
1
2
3
4
msvc 编译器出错(已翻译)
src/main.cpp(35): error C3520: 'T': the parameter pack must be expanded in this context
src/main.cpp(37): error C3536: '<begin>$L0': can't be used before initialization
src/main.cpp(37): error C3536: '<end>$L0': can't be used before initialization
src/main.cpp(37): error C2100: invalid redirection
【问题讨论】:
-
int_printf(3, nullptr, -9e134, "hello", main);呢,vec的类型是什么? -
那很痛????,我知道问题应该出在 std::vector
但我不知道如何使它仅适用于整数。
标签: c++ templates variadic-templates