Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
military-order.hh
Go to the documentation of this file.
1 #ifndef VCSN_MISC_MILITARY_ORDER_HH
2 # define VCSN_MISC_MILITARY_ORDER_HH
3 
4 # include <tuple>
5 # include <type_traits>
6 
7 namespace vcsn
8 {
9 
10  // Is it possible to write a C++ template to check for a function's
11  // existence? http://stackoverflow.com/questions/257288
12  namespace detail
13  {
14  template <typename>
15  struct sfinae_true : std::true_type {};
16 
17  template <typename T>
18  static auto test_size(int)
20 
21  template <typename>
22  static auto test_size(long) -> std::false_type;
23  }
24 
26  template <typename T>
28  : decltype(detail::test_size<T>(0))
29  {};
30 
37  template <typename T>
39  {
40  public:
41  bool operator()(const T& x, const T& y) const
42  {
43  return lt_<T>(x, y);
44  }
45 
46  private:
48  template <typename T2>
49  auto lt_(const T2& x, const T2& y) const
50  -> typename std::enable_if<has_size_member_function<T2>::value, bool>::type
51  {
52  return
53  std::forward_as_tuple(x.size(), x) < std::forward_as_tuple(y.size(), y);
54  }
55 
57  template <typename T2>
58  auto lt_(const T2& x, const T2& y) const
59  -> typename std::enable_if<!has_size_member_function<T2>::value, bool>::type
60  {
61  return x < y;
62  }
63  };
64 
65 }
66 
67 #endif // !VCSN_MISC_MILITARY_ORDER_HH
auto lt_(const T2 &x, const T2 &y) const -> typename std::enable_if<!has_size_member_function< T2 >::value, bool >::type
Case where T does not feature a size() member function.
static auto test_size(int) -> sfinae_true< decltype(std::declval< T >().size())>
Whether T features a size() function.
bool operator()(const T &x, const T &y) const
Military strict order predicate.
auto lt_(const T2 &x, const T2 &y) const -> typename std::enable_if< has_size_member_function< T2 >::value, bool >::type
Case where T features a size() member function.