The following code seems to be a decent SFINAE way to check for the existence of a method with no parameters:
template<typename T> struct has_size_method {
private:
typedef std::true_type yes;
typedef std::false_type no;
template<typename U> static auto test(int)
-> decltype(std::declval<U>().size() == 1, yes());
template<typename> static no test(...);
public:
static constexpr bool value = std::is_same<decltype(test<T>(0)),yes>::value;
};
(lifted from here). How do I adapt that for taking a parameter? The following
template<typename T, typename Key> struct has_find_method {
private:
typedef std::true_type yes;
typedef std::false_type no;
template<typename U> static auto test(int)
-> decltype(std::declval<U>().find(std::declval<const Key&>()) == 1, yes());
template<typename> static no test(...);
public:
static constexpr bool value = std::is_same<decltype(test<T>(0)),yes>::value;
};
doesn't work, although it does compile (GodBolt). What am I doing wrong?
Note: While this question stands on its own merit (I believe), it's also an X-Y question. I need a C++11-compatible find function which also works for maps (but without using Boost or something heavy like that.)
Aucun commentaire:
Enregistrer un commentaire