小编Zis*_*ack的帖子

如何构造一个类型特征来判断一种类型的私有方法是否可以在另一种类型的构造函数中调用?

我正在使用 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)

c++ templates type-traits c++17

7
推荐指数
2
解决办法
172
查看次数

标签 统计

c++ ×1

c++17 ×1

templates ×1

type-traits ×1