如何在C++17中使用type_traits检测具有特定名称和签名的函数(NOT类成员)的存在
How to detect the existence of a function (NOT class member) with a specific name and signature using type_traits in C++17
尝试完成以下操作:
// Template not necessary, but shows the pattern
template <typename T>
bool MyFunction(const T&, const uint8_t);
template <T>
struct is_myfunc_defined : std::false_type{}
// How do I properly create this
template <typname R, typename... Args>
struct is_myfunc_defined<R MyFunction(args....)> : std::true_type
{
};
struct MyStruct1 {};
struct MyStruct2 {};
// Will Match
bool MyFunction(const MyStruct&, const uint8_t){ return true; }
// Will not match
bool ShouldFail1(const MyStruct2&, const uint8_t){ return true; }
void MyFunction(const MyStruct2&, const uint8_t){ return true; }
bool MyFunction(const MyStruct2&){ return true; }
int main()
{
cout << is_myfunc_defined<MyStruct>::value << endl; // true
cout << is_myfunc_defined<MyStruct2>::value << endl; // false
}
我知道如何使用is_detected_class来检查具有特定返回类型、名称和签名的类方法,但如何使用直接函数来检查。想不通,需要帮助。
谢谢!
我知道如何使用
is_detected_exact
检查类方法
对于全局函数来说没有什么不同:
template <typename ...P>
using detect_myfunc = decltype(MyFunction(std::declval<P>()...));
template <typename T>
struct is_myfunc_defined {};
template <typename R, typename ...P>
struct is_myfunc_defined<R(P...)>
: std::experimental::is_detected_exact<R,detect_myfunc,P...> {};
{};
相关文章:
- 有没有办法将重载的类函数绑定到函数对象?
- ROS 订阅回调 - 使用 boost::绑定成员函数
- 为什么我们需要 & in 绑定成员函数?
- 如何将参数递归绑定到函数?
- 如何发送通过绑定到函数/方法创建的函数对象?
- 如何修改 std::generate 的绑定成员函数的结果
- 当Boost ::绑定模板函数时,模板参数扣除/替换失败
- 如何正确绑定成员函数与 boost::bind
- C2276 '&':对绑定成员函数表达式的非法操作
- 尝试将实例传递到绑定的函数C
- 尝试将字符串传递到绑定的函数C
- 为线程构造函数传递引用以将其绑定到函数失败
- 为什么c++中线程构造函数和绑定的函数签名是相同的
- 绑定重载函数
- 模板成员转换运算符,默认模板参数到与 TypeName 绑定的函数
- 使用带有 boost::optional" 参数与 boost::spirit 绑定成员函数
- boost::绑定静态函数的参数
- C++11绑定std::函数与存储元组和解包
- 模板类重载std::绑定成员函数
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁