Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- template<typename T>
- struct Method {};
- template<>
- struct Method<int> {
- int operator()() { return 1; }
- };
- template<class T>
- struct is_brackets_op_defined {
- static void Check(...);
- template<typename C>
- static decltype(Method<C>::operator()) Check(const C&);
- using type = decltype(Check(
- std::declval< Method<T> >()
- ));
- constexpr static bool value =
- !std::is_same<void, type>();
- };
- int main() {
- std::cout << is_brackets_op_defined<float>::value << std::endl;
- }
- std::cout << is_brackets_op_defined<float>::value // false
- std::cout << is_brackets_op_defined<int >::value // false
- #include <iostream>
- #include <functional>
- template<typename T>
- struct Method {};
- template<>
- struct Method<int> {
- int operator()() { return 1; }
- };
- template<class T>
- struct is_brackets_op_defined {
- static std::false_type Check(...);
- template<typename C, class = decltype(&C::operator())>
- static std::true_type Check(const C*);
- using type = decltype(Check(static_cast<Method<T>*>(nullptr)));
- constexpr static bool value = type();
- };
- int main() {
- std::cout << std::boolalpha << is_brackets_op_defined<float>::value << std::endl;
- std::cout << std::boolalpha << is_brackets_op_defined<int>::value << std::endl;
- }
- template<typename C>
- static decltype(std::declval<C>().operator()()) Check(const C&);
- #include <iostream>
- #include <type_traits>
- template<class T> struct
- is_brackets_op_defined
- {
- private: template<typename C> static
- int Check(...);
- private: template<typename C> static
- void Check(decltype(&C::operator ()));
- public: constexpr static bool value
- {
- not std::is_same_v<int, decltype(Check<T>(nullptr))>
- };
- };
- struct ClassWith
- {
- void operator ()() {}
- };
- struct ClassWithout
- {
- };
- int main()
- {
- std::cout << is_brackets_op_defined<float>::value << std::endl;
- std::cout << is_brackets_op_defined<ClassWith>::value << std::endl;
- std::cout << is_brackets_op_defined<ClassWithout>::value << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment