c++ - 使用T的默认构造函数作为默认初始值

标签 c++ templates default-value default-constructor function-declaration

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/

相关文章:

c++ - 对于复合数据类型使用 glClearBufferData 是否可移植?

c++ - 包含常量类型模板的类的模板?

c++ - 另一个文件 c++ 中的模板特化。哪个版本得到

javascript - 保留元素的 JS 模板引擎

Python Empty Iterable 作为默认参数

python - 设置应该是 Python 中列表的参数的默认值的最佳实践?

c++ - 将 char 直接放在数组后的可靠方法

c++ - 无法将图像保存为白色背景 OpenCV 的 JPG

c++ - 为模板类静态实例生成有效标识符的宏

c# - 传递空数组作为可选参数的默认值