C++ 11查找类型是否具有成员函数或支持运算符的方法?

标签 c++ c++11

有一种(看似)不错的 C++03 方法可以确定一个类型是否具有成员函数或运算符:

https://github.com/jaredhoberock/is_call_possible/blob/master/is_call_possible.hpp

没有现代 C++11 方法可以做到这一点吗?最好不要包含任何外部代码而只使用标准。

最佳答案

这适用于 GitHub 中给出的所有测试用例(演示:http://ideone.com/ZLGp4R):

#include <type_traits>

template <typename C, typename F, typename = void>
struct is_call_possible : public std::false_type {};

template <typename C, typename R, typename... A>
struct is_call_possible<C, R(A...),
    typename std::enable_if<
        std::is_same<R, void>::value ||
        std::is_convertible<decltype(
            std::declval<C>().operator()(std::declval<A>()...)
            //                ^^^^^^^^^^ replace this with the member you need.
        ), R>::value
    >::type
> : public std::true_type {};

关于C++ 11查找类型是否具有成员函数或支持运算符的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17201329/

相关文章:

c++ - 关于删除指针的问题。它应该从哪个类中删除?

c++ - 非贪婪 C++ 正则表达式的问题

c++ - Regex C 关于转义字符的帮助

c++ - 如何判断库是使用 C++11 编译的

C++11 构造函数

c++ - boost::stable_vector 插入比 std::vector 慢几个数量级。为什么?

C++11 字符串的分配要求

c++ - sqlite3_prepare_v2 返回错误 1 ​​代码

c++ - 将 vector 指针传递给函数会导致段错误

sorting - C++ std::const 结构的排序