小编Jas*_*ton的帖子

从静态方法等效到decltype(*this)?

我有一些宏需要访问当前类的类型,我现在通过DRY违反模式逃脱:

struct ThisScruct{
    int a;
    double b;
    //example static method using this - purely example - not full usecase

    static size_t sum_offsets(){
       typedef ThisStruct SelfT;
       return offsetof(SelfT, a) + offsetof(SelfT, b);
    }
};
Run Code Online (Sandbox Code Playgroud)

使用offsetof关键字会产生很多,至少在我自己的工作中.

现在,在锁定this无法通过静态方法访问之前- 实现我只想知道如何ThisStruct从静态方法上下文以通用/宏友好的方式获取类型.我实际上并不需要/想要一个实例,我正在寻找实际上像上面那样没有typedeffing的方式SelfT.

编辑:我可以self在C++中实现自治成员类型吗?- 但我担心钻石问题形成的类都继承了接受的答案Self类.

c++ c++11

9
推荐指数
1
解决办法
852
查看次数

标签 统计

c++ ×1

c++11 ×1