Opened 7 years ago

Last modified 5 years ago

#12081 new Bugs

the minmax creates dangling references too easily

Reported by: anonymous Owned by: Marshall Clow
Milestone: To Be Determined Component: minmax
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc:

Description

First. minmax's example codes has created dangling references.

boost::tuple<int const&, int const&> result1 = boost::minmax(1, 0); //actual code
boost::tuple<int, int> result1 = boost::minmax(1, 0); //expected code

http://www.boost.org/doc/libs/1_60_0/libs/algorithm/minmax/index.html
http://www.boost.org/doc/libs/1_60_0/libs/algorithm/minmax/example/minmax_ex.cpp


Second. The design of minmax is unsuitable for that of C++11 era. Consider the following.

auto tp = minmax(1,2);
tp.get<0>(); // Undefined behavior

This code looks like fairy normal, but it causes undefined behavior. Since the minmax returns tuple<T const&, T const&>, the 'auto tp' has dangling references.

I think we should change the return type from tuple<T const&, T const&> to tuple<T,T>.

See also: https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/HLiJJBRNYSw

Change History (2)

comment:1 by Marshall Clow, 5 years ago

This is the same interface that std::min has.

int const &x = std::min(0,1);

comment:2 by Marshall Clow, 5 years ago

I think we should change the return type from tuple<T const&, T const&> to tuple<T,T>.

That would add the requirement that T be copyable.

Note: See TracTickets for help on using tickets.