forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_less_than_comparable.h
74 lines (51 loc) · 1.96 KB
/
is_less_than_comparable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <type_traits>
#include "drake/common/unused.h"
namespace drake {
#ifndef DRAKE_DOXYGEN_CXX
namespace is_less_than_comparable_internal {
// Default case; assumes that a class is *not* less-than comparable.
template <typename T, typename = void>
struct is_less_than_comparable_helper : std::false_type {};
// Special sauce for SFINAE. Only compiles if it can finds the method
// `operator<`. If this exists, the is_less_than_comparable implicitly
// prefers this overload over the default overload.
template <typename T>
struct is_less_than_comparable_helper<T, typename std::enable_if_t<true,
decltype(unused(std::declval<T&>() < std::declval<T&>()),
(void)0)>> : std::true_type {};
} // namespace is_less_than_comparable_internal
/** @endcond */
/**
@anchor is_less_than_comparable_doc
Provides method for determining at run time if a class is comparable using
the less-than operator (<).
__Usage__
This gets used like `type_traits` functions (e.g., `is_copy_constructible`,
`is_same`, etc.) To determine if a class is less-than comparable simply invoke:
@code
bool value = drake::is_less_than_comparable<Foo>::value;
@endcode
If `Foo` is less-than comparable, it will evaluate to true. It can also be used
in compile-time tests (e.g., SFINAE and `static_assert`s):
@code
static_assert(is_less_than_comparable<Foo>::value, "This method requires its "
"classes to be less-than comparable.");
@endcode
__Definition of "less-than comparability"__
To be less-than comparable, the class `Foo` must have a public method of the
form:
@code
bool Foo::operator<(const Foo&) const;
@endcode
or a definition external to the class of the form:
@code
bool operator<(const Foo&, const Foo&);
@endcode
@tparam T The class to test for less-than comparability.
*/
template <typename T>
using is_less_than_comparable =
is_less_than_comparable_internal::is_less_than_comparable_helper<T, void>;
} // namespace drake
#endif