ein*_*ica -1 c++ sfinae template-meta-programming c++11
下面的代码似乎是一种不错的 SFINAE 方法来检查没有参数的方法是否存在:
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;
};
Run Code Online (Sandbox Code Playgroud)
(从这里解除)。我如何适应它以获取参数?下列
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;
};
Run Code Online (Sandbox Code Playgroud)
不起作用,尽管它可以编译(GodBolt)。我究竟做错了什么?
注意:虽然这个问题有其自身的优点(我相信),但它也是一个 XY 问题。我需要一个 C++11 兼容的 find 函数,它也适用于地图(但不使用 Boost 或类似的东西。)