【发布时间】:2018-04-23 12:24:03
【问题描述】:
我正在编写一个模板函数,它使用std::minus 返回向量的连续元素的差异。
目前我有以下代码:
template <typename T>
auto diffs(std::vector<T> &nums)
{
typedef decltype(std::minus<T>()) diffType;
std::vector<diffType> differences;
differences.reserve(nums.size() - 1);
auto test = std::back_inserter<std::vector<decltype (std::minus<T>())>>;
std::transform(++begin(nums), end(nums), begin(nums), std::back_inserter(differences), std::minus<T>());
return differences;
}
并使用
调用它void test()
{
auto values = std::vector<int>{ 1,2,4,6,89,1456 };
auto differences = diffs(values);
}
当我编译这个时,我得到:
binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
如何声明我的typedef 以便它与我的back_inserter 正常工作?
【问题讨论】:
标签: c++ templates c++17 type-deduction