Created on 2010-08-25.00:00:00 last changed 161 months ago
Proposed resolution:
template<class RandomAccessIterator, class UniformRandomNumberGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& rand);
template<class RandomAccessIterator, class UniformRandomNumberGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& rand);
[ Adopted at 2010-11 Batavia ]
[ Post-Rapperswil, Daniel provided wording ]
The signatures of the shuffle and random_shuffle algorithms are different in regard to the support of rvalues and lvalues of the provided generator:
template<class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator&& rand);
template<class RandomAccessIterator, class UniformRandomNumberGenerator> void shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator& g);
The first form uses the perfect forwarding signature and that change compared to C++03 was done intentionally as shown in the first rvalue proposal papers.
While it is true, that random generators are excellent examples of stateful functors, there still exist good reasons to support rvalues as arguments:
Arguments have been raised that accepting rvalues is error-prone or even fundamentally wrong. The author of this proposal disagrees with that position for two additional reasons:
instead of writingmy_generator get_generator(int size);
they will just writestd::vector<int> v = ...; std::shuffle(v.begin(), v.end(), get_generator(v.size()));
and feel annoyed about the need for it.std::vector<int> v = ...; auto gen = get_generator(v.size()); std::shuffle(v.begin(), v.end(), gen);
Thus this proposal recommends to make both shuffle functions consistent and perfectly forward-able.
Moved to Tentatively Ready after 6 positive votes on c++std-lib.
Duplicate: 1433
Addresses US-121, GB-119
random_shuffle and shuffle should be consistent in how they accept their source of randomness: either both by rvalue reference or both by lvalue reference.
History | |||
---|---|---|---|
Date | User | Action | Args |
2011-08-23 20:07:26 | admin | set | status: wp -> c++11 |
2010-11-24 14:01:03 | admin | set | messages: + msg5431 |
2010-11-14 13:10:57 | admin | set | status: voting -> wp |
2010-11-08 14:14:39 | admin | set | status: ready -> voting |
2010-10-21 19:47:27 | admin | set | messages: + msg4849 |
2010-10-21 19:47:27 | admin | set | messages: + msg4848 |
2010-08-25 00:00:00 | admin | create |