diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-07-01 17:47:54 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-07-01 17:47:54 +0200 |
commit | 38c8f0efd8f033be8fb8278aa5d0eb704dedee55 (patch) | |
tree | d06d01988c0cab4ecc82e170fd520d6acbdbc710 | |
parent | dc89d29d65b6f383fbaea9a973c4e7b96723df9d (diff) |
Improve reverse_iterate implementation
If the passed range is an rvalue, then move it into our private copy.
Otherwise, with nested ranges, there is no guarantee the thing will
still be alive once we get to iterating over it.
-rw-r--r-- | butl/utility | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/butl/utility b/butl/utility index b1fa8f6..6efa4d0 100644 --- a/butl/utility +++ b/butl/utility @@ -5,6 +5,7 @@ #ifndef BUTL_UTILITY #define BUTL_UTILITY +#include <utility> // forward() #include <cstring> // strcmp namespace butl @@ -32,22 +33,18 @@ namespace butl template <typename T> class reverse_range { - T& x_; + T x_; public: - reverse_range (T& x): x_ (x) {} + reverse_range (T&& x): x_ (std::forward<T> (x)) {} - auto begin () -> decltype (this->x_.rbegin ()) {return x_.rbegin ();} - auto end () -> decltype (this->x_.rend ()) {return x_.rend ();} + auto begin () const -> decltype (this->x_.rbegin ()) {return x_.rbegin ();} + auto end () const -> decltype (this->x_.rend ()) {return x_.rend ();} }; template <typename T> inline reverse_range<T> - reverse_iterate (T& x) {return reverse_range<T> (x);} - - template <typename T> - inline reverse_range<const T> - reverse_iterate (const T& x) {return reverse_range<const T> (x);} + reverse_iterate (T&& x) {return reverse_range<T> (std::forward<T> (x));} } #endif // BUTL_UTILITY |