Write a function template that takes a single type parameter (T) and accepts four function arguments: an array of T, a start index, a stop index (inclusive), and an optional initial value. The function returns the sum of all the array elements in the specified range and the initial value. Use the default constructor of T for the default initial value. Repeat the exercise but use explicit to manually create specializations for int data type.
上面粗体行的含义是什么?
如何拥有T
的默认构造函数?
最佳答案
它的意思类似于下面
template <typename T>
T sum( const T a[], size_t start, size_t stop, const T &init = T() );
其中第四个参数的默认值是使用 T
类型的默认构造函数创建的。
关于c++ - 使用T的默认构造函数作为默认初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68806376/