[英]How do I return by value for an “auto” return type function
好的,我不知道我对标题的解释是否很好,所以我只举一个例子。 我试图提出一个函数,该函数可以找到一系列浮点数的中位数(还有一些额外的值)。 这是函数:
//get the median of an unordered set of numbers of arbitrary type without modifying the
//underlying dataset
template <typename InputIterator>
auto Median(
InputIterator const cbegin,
InputIterator const cend,
bool const abs = false // get the median of the absolute values rather than nominal values
) -> decltype(*cbegin)
{
typedef std::iterator_traits<InputIterator>::value_type T;
std::vector<T> data;
data.reserve(std::distance(cbegin, cend));
// get a copy of the values for sorting
for (InputIterator it = cbegin; it != cend; ++it)
{
if (abs)
data.push_back(std::abs(*it));
else
data.push_back(*it);
}
// find the median
std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end());
return data[data.size() / 2];
}
如果我尝试编译此,则为以下输出:
警告C4172:返回局部变量或临时地址
我试图替换decltype(*cbegin)
与std::remove_reference<decltype(*cbegin)>::type
或std::iterator_traits<decltype(*cbegin)>::value_type
但那些不进行编译任一。
有一个简单的解决方案吗? 如何返回InputIterator
指向的类型?
编辑:这是基于反馈的固定版本:
//get the median of an unordered set of numbers of arbitrary type without modifying the
//underlying dataset
template <typename RandomAccessIterator>
typename std::iterator_traits<RandomAccessIterator>::value_type Median(
RandomAccessIterator const cbegin,
RandomAccessIterator const cend,
bool const abs = false // get the median of the absolute values rather than nominal values
)
{
typedef std::iterator_traits<RandomAccessIterator>::value_type T;
std::vector<T> data(cbegin, cend);
// find the median
std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end(),
[abs](T const a, T const b)
{
return abs ? std::abs(b) > std::abs(a) : b > a;
});
return data[data.size() / 2];
}
您可以将iterator_traits
与模板参数一起使用,而无需decltype:
template <typename InputIterator>
typename std::iterator_traits<InputIterator>::value_type
Median(
InputIterator const cbegin,
InputIterator const cend,
bool const abs = false
);
请注意typename
关键字-这是您尝试中缺少的内容。
编译器告诉您,您实际上正在返回一些临时信息。
decltype(* cbegin)是一个引用(因此您可以编写*it = 12
类的代码),因此您将返回对data
内部临时值的引用。
您可以使用std::remove_reference<decltype(*cbegin)>::type
删除引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.