| Line | |
|---|
| 1 | #ifndef IS_CALLABLE_H |
|---|
| 2 | #define IS_CALLABLE_H |
|---|
| 3 | |
|---|
| 4 | #include <type_traits> |
|---|
| 5 | |
|---|
| 6 | // See https://stackoverflow.com/a/15396757/7421666 |
|---|
| 7 | template<typename T> |
|---|
| 8 | struct IsCallableImpl |
|---|
| 9 | { |
|---|
| 10 | private: |
|---|
| 11 | typedef char(&yes)[1]; |
|---|
| 12 | typedef char(&no)[2]; |
|---|
| 13 | |
|---|
| 14 | struct Fallback { void operator()(); }; |
|---|
| 15 | struct Derived : T, Fallback { }; |
|---|
| 16 | |
|---|
| 17 | template<typename U, U> struct Check; |
|---|
| 18 | |
|---|
| 19 | template<typename> |
|---|
| 20 | static yes test(...); |
|---|
| 21 | |
|---|
| 22 | template<typename C> |
|---|
| 23 | static no test(Check<void (Fallback::*)(), &C::operator()>*); |
|---|
| 24 | |
|---|
| 25 | public: |
|---|
| 26 | static const bool value = sizeof(test<Derived>(0)) == sizeof(yes); |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | // Checks if a type is callable (has an 'operator()'). This will not work for |
|---|
| 30 | // normal functions without modifications, but we don't need that case anyway. |
|---|
| 31 | template<typename T> |
|---|
| 32 | struct IsCallable |
|---|
| 33 | : std::conditional< |
|---|
| 34 | std::is_class<T>::value, |
|---|
| 35 | IsCallableImpl<T>, |
|---|
| 36 | std::false_type |
|---|
| 37 | >::type |
|---|
| 38 | { }; |
|---|
| 39 | |
|---|
| 40 | #endif // IS_CALLABLE_H |
|---|
Note: See
TracBrowser
for help on using the repository browser.