创建模板参数的默认值变量

Rot*_*tem 3 c++ templates

以下面的求和模板函数为例:

template <typename T>
T Sum(std::vector<T>& source)
{
    T v;
    for (auto it = source.begin(); it != source.end(); ++it)
    {
        v += *it;
    }
    return v;
}
Run Code Online (Sandbox Code Playgroud)

这适用于定义+=运算符的自定义类型,但是我如何才能使它适用于基本类型,例如float

该行将T v;产生未初始化的值.

使用T v = 0;将起作用float,但不适用于非基元.