使用C ++的所有新功能(我认为C ++ 11就足够了),可以防止使用std::minmax
函数返回一对引用。
通过这种方式,如果一个提供两个可修改的引用,则可以修改它们。这个开口是一堆蠕虫吗?
#include<functional>
// maybe all this options can be simplified
template<class T1, class T2> struct common;
template<class T> struct common<T, T>{using type = T;};
template<class T> struct common<T const&, T&>{using type = T const&;};
template<class T> struct common<T&, T const&>{using type = T const&;};
template<class T> struct common<T, T&>{using type = T const&;};
template<class T> struct common<T&, T>{using type = T const&;};
template<class T> struct common<T const&, T>{using type = T const&;};
template<class T> struct common<T, T const&>{using type = T const&;};
template<class T1, class T2, class Compare = std::less<>, class Ret = typename common<T1, T2>::type>
std::pair<Ret, Ret> minmax(T1&& a, T2&& b, Compare comp = {}){
return comp(b, a) ?
std::pair<Ret, Ret>(std::forward<T2>(b), std::forward<T1>(a))
: std::pair<Ret, Ret>(std::forward<T1>(a), std::forward<T2>(b));
}
测试:
#include<cassert>
int main(){
{
int a = 1;
int b = 10;
auto& small = minmax(a, b).first;
assert(small == 1);
small += 1;
assert(a == 2);
}{
int const a = 1;
int b = 10;
auto& small = minmax(a, b).first;
assert(small == 1);
// small += 1; error small is const reference, because a was const
}{
int a = 1;
int const b = 10;
auto& small = minmax(a, b).first;
assert(small == 1);
// small += 1; error small is const reference, because a was const
}{
int const a = 1;
int const b = 10;
auto& small = minmax(a, b).first;
assert(small == 1);
// small += 1; error small is const reference, because a was const
}{
int b = 10;
auto& small = minmax(int(1), b).first;
assert(small == 1);
// small += 1; error small is const reference, because first argument was const
}{
int a = 1;
auto& small = minmax(a, int(10)).first;
assert(small == 1);
// small += 1; error small is const reference, because second argument was const
}
{
int const a = 1;
auto& small = minmax(a, int(10)).first;
assert(small == 1);
// small += 1; error small is const reference, because both arguments are const
}
{
// auto& small = minmax(int(1), int(10)).first; // error, not clear why
auto const& small = minmax(int(1), int(10)).first; // ok
// auto small2 = minmax(int(1), int(10)).first; // also ok
assert(small == 1);
// small += 1; error small is const reference, because both arguments are const
}
}
答案 0 :(得分:1)
很久以前,Howard Hinnant:N2199就曾有过类似的论文。其开头的示例演示了您要解决的确切问题:
该功能不能在作业的左侧使用:
int x = 1; int y = 2; std::min(x, y) = 3; // x == 3 desired, currently compile time error
接着列举了频繁出现的参考问题,混合类型以及对仅移动类型有用的示例,并继续提出了解决所有问题的min
和max
新版本这些问题-它在底部包括一个非常全面的实现(太长了,无法在此处粘贴)。基于此实现minmax()
应该非常简单:
template <class T, class U,
class R = typename min_max_return<T&&, U&&>::type>
inline
std::pair<R, R>
minmax(T&& a, U&& b)
{
if (b < a)
return {std::forward<U>(b), std::forward<T>(a)};
return {std::forward<T>(a), std::forward<U>(b)};
}
当时论文被拒绝了。可能它会回来。
能够取回可变引用很不错,但是能够避免 dangling 引用则更好。匿名引用我最近看到的一个例子:
template<typename T> T sign(T); template <typename T> inline auto frob(T x, T y) -> decltype(std::max(sign(x - y), T(0))) { return std::max(sign(x - y), T(0)); }
此函数对所有输入均具有不确定的行为(最窄 合同可能吗?)。
请注意,您的common
实现存在此问题。这些情况:
template<class T> struct common<T, T&>{using type = T const&;}; template<class T> struct common<T&, T>{using type = T const&;}; template<class T> struct common<T const&, T>{using type = T const&;}; template<class T> struct common<T, T const&>{using type = T const&;};
所有悬挂。如果我有这意味着什么?
int i = 4;
auto result = your_minmax(i, 5);
result
这里是pair<int const&, int const&>
,其中一个是对i
的引用,另一个是悬挂的。为了安全起见,所有这些情况都必须执行using type = T;
。