| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

reverse.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_REVERSE_H
2 #define GAUDIKERNEL_REVERSE_H
3 //
4 // provide a generic 'reverse' function for use in range-based for loops.
5 //
6 // example:
7 //
8 // #include "GaudiKernel/reverse.h"
9 // auto l = { 1,2,3,4 };
10 // for ( const auto& i : reverse( l ) ) std::cout << i << std::endl;
11 //
12 // Note that it is perfectly fine to reverse a temporary. The temporary
13 // will be moved into the reverse_wrapper, and thus kept alive for the
14 // duration of the loop. In case reverse is called on an lvalue, the wrapper
15 // will take a reference, so no copy will be performed. (if you wonder how
16 // the code below (very implicitly) distinguishes between the two cases,
17 // google for 'C++11 reference collapsing' -- short version: C++ does not
18 // allow references to references, so in cases where this would happen,
19 // one gets an lvalue reference)
20 //
21 // Also note that reverse_wrapper does not have a constructor, but this
22 // implies that it does meet the requirements for aggregate initializaton,
23 // which allows for {} initialization of its member.
24 //
25
26 template <typename Iterable>
27 struct reverse_wrapper { Iterable iterable; };
28
29 template <typename T>
30 reverse_wrapper<T> reverse(T&& iterable) { return { std::forward<T>(iterable) }; }
31
32
33 #if defined __GNUC__ && __GNUC__ < 5
34
35 // std::rbegin and std::rend require gcc 5.0 or later (or clang 3.5 or later)
36 template <typename T>
37 auto begin(reverse_wrapper<T>& w) -> decltype(w.iterable.rbegin()) { return w.iterable.rbegin(); }
38 template <typename T>
39 auto end(reverse_wrapper<T>& w) -> decltype(w.iterable.rend()) { return w.iterable.rend(); }
40
41 #else
42
43 #include <iterator>
44 template <typename T>
45 auto begin(reverse_wrapper<T>& w) { using std::rbegin; return rbegin(w.iterable); }
46 template <typename T>
47 auto end(reverse_wrapper<T>& w) { using std::rend; return rend(w.iterable); }
48
49 #endif
50
51
52 #endif
reverse_wrapper::iterable
Iterable iterable
Definition: reverse.h:27
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
reverse_wrapper
Definition: reverse.h:27
reverse
reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:30