我正在使用 C++17。我有如下代码:
#include <type_traits>
template <typename T>
struct Fooer
{
Fooer (T & fooable)
{
fooable . foo ();
}
};
template <typename T>
Fooer (T & fooable) -> Fooer <T>;
struct Fooable
{
private:
void
foo ();
friend struct Fooer <Fooable>;
};
struct NotFooable
{
};
Run Code Online (Sandbox Code Playgroud)
我想实现一个可以判断类型是否为“Fooable”的类型特征。
我无法检查foo ()该类型是否有方法,因为它是私有方法。这也没有告诉我Fooer的构造函数是否可以调用该方法。
// Checking for the foo method doesn't work.
template <typename T, typename = void>
struct HasFoo;
template <typename T, typename>
struct HasFoo : std::false_type
{
}; …Run Code Online (Sandbox Code Playgroud)