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