繁体   English   中英

如何使用 C++17 中的 type_traits 检测具有特定名称和签名的 function(不是 class 成员)的存在

[英]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_exact 来检查具有特定返回类型、名称和签名的 class 方法,但是如何使用直接的 function 来执行此操作。 想不通,求帮助。

谢谢!

我知道如何使用is_detected_exact检查 class 方法

全局 function 也不例外:

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...> {};
{};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM