How to detect whether there is a specific PRIVATE member variable in class?

后端 未结 2 506
鱼传尺愫
鱼传尺愫 2021-02-06 16:18

This question is based on this post.

Goal: I would like to know if a class has the member variable x. I would like to receive true

2条回答
  •  轮回少年
    2021-02-06 16:55

    The following seems to work. Please tell me if it needs improvement. Live example.

    class Haser {
    public:
    template 
    static constexpr bool HasX = false;
    
    template  
    static constexpr bool HasX = true;
    };
    
    struct A { 
    private:    
        int x;
        friend Haser;
    };
    

    Haser::HasX is true.

提交回复
热议问题