fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <vector>
  4. #include <set>
  5.  
  6. template<typename, typename T>
  7. struct has_insert_method
  8. {
  9. static_assert(std::integral_constant<T, false>::value,
  10. "Second template parameter needs to be of function type.");
  11. };
  12.  
  13. template<typename C, typename Ret, typename... Args>
  14. struct has_insert_method<C, Ret(Args...)>
  15. {
  16. private:
  17. template<typename T>
  18. static constexpr auto check(T*) -> typename std::is_same<decltype(std::declval<T>().insert(std::declval<Args>()...)), Ret>::type;
  19.  
  20. template<typename>
  21. static constexpr std::false_type check(...);
  22.  
  23. typedef decltype(check<C>(0)) type;
  24.  
  25. public:
  26. static constexpr bool value = type::value;
  27. };
  28.  
  29. int main()
  30. {
  31. typedef std::vector<int> v;
  32. std::cout << "Does vector have an insert method?: " << has_insert_method<v, std::pair<v::iterator, bool>(v::value_type const&)>::value;
  33. std::cout << std::endl;
  34.  
  35. typedef std::set<int> t;
  36. std::cout << "Does set have an insert method?: " << has_insert_method<t, std::pair<t::iterator, bool>(t::value_type const&)>::value;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Does vector have an insert method?: 0
Does set have an insert method?: 1