lar*_*moa 1 c++ templates variadic-functions
我有一个可变参数模板函数:
template<typename T, typename ArgType>
vector<T>
createVector(const int count, ...)
{
vector<T> values;
va_list vl;
va_start(vl, count);
for (int i=0; i < count; ++i)
{
T value = static_cast<T>(va_arg(vl, ArgType));
values.push_back(value);
}
va_end(vl);
return values;
}
Run Code Online (Sandbox Code Playgroud)
这适用于T和ArgType的一些(对我来说,奇怪的)配置,但不是我期望的方式:
// v1 = [0.0, 1.875, 0.0]
vector<float> v1 = createVector<float, float>(3, 1.0f, 2.0f, 3.0f);
// v2 = [0.0, 1.875, 0.0]
vector<float> v2 = createVector<float, float>(3, 1.0, 2.0, 3.0);
// v3 = [1.0, 2.0, 3.0]
vector<float> v3 = createVector<float, double>(3, 1.0, 2.0, 3.0);
// v4 = [1.0, 2.0, 3.0]
vector<float> v4 = createVector<float, double>(3, 1.0f, 2.0f, 3.0f);
// v5 = [1.0, 2.0, 3.0]
vector<double> v5 = createVector<double, double>(3, 1.0, 2.0f, 3.0);
Run Code Online (Sandbox Code Playgroud)
为什么当ArgType为double时(即使在传递浮点数时),这是有效的,但是当它浮动时却不行?