在对象声明上下文中获取此类型

时间:2015-08-01 17:43:39

标签: c++ templates c++11 metaprogramming c++14

至于我所做的研究,没有好的方法可以在静态上下文中获取封闭类的类型。那么非静态背景呢。

示例:

template<typename TOwner, typename T>
struct prop {
    typedef TOwner Owner;

    prop(TOwner* owner) {
    }
};

struct MyType {
    prop<MyType, short> my_prop = {this};
    prop<decltype(my_prop)::Owner, long> prp = {this};
};

这段代码很棒。但是尝试用MyType替换decltype(*this)并停止工作,说这不能在静态上下文中声明。但是my_prop也不能被视为静态上下文,并且或多或少与此相同(可以重新定义为this-> my_prop)。

所以我的问题如下:我是否遗漏了某些内容,或者在声明属性时,即使在非静态上下文中也无法获得this的类型?

1 个答案:

答案 0 :(得分:1)

有点偏离主题,但是如果要将Owner指针存储在Owner的每个属性成员中,那么就内存而言,这是次优的,即每个属性成员都有一个副本同样的Owner*

在C ++中,对象布局在编译时是已知的,因此,对于this成员的Owner指针,可以使用offsetof macro获取Owner*,提供会员名称已知。

仍然,要将数据成员名称注入Owner,必须定义数据成员,并且Owner中至少需要一个字节,因为C ++不允许0个大小的数据成员(不像空基类优化)。

以下是基于使用offsetof

的示例
#include <utility>
#include <cstddef>
#include <cstdint>

template<class Owner, class T, class Tag = void>
struct Property;

template<class Owner, class T, class Tag>
Owner& get_property_owner(Property<Owner, T, Tag>&); // Must be overloaded for each individual property of a class.

template<class Owner, class T, class Tag>
inline Owner const& get_property_owner(Property<Owner, T, Tag> const& p) {
    return get_property_owner(const_cast<Property<Owner, T, Tag>&>(p));
}

template<class Owner, class T, class Tag>
struct Property
{
    Property() = default;
    Property(Property const&) = delete;
    Property& operator=(Property const&) = delete;

    template<class U>
    Property& operator=(U&& u) {
        get_property_owner(*this).property_set(*this, std::forward<U>(u));
        return *this;
    }

    operator T() const {
        return get_property_owner(*this).property_get(*this);
    }
};

// Convenience macro to save typing boiler plate code.
#define PROPERTY(Owner, Type, Name) \
struct PropertyName_##Name {}; \
Property<Owner, Type, PropertyName_##Name> Name; \
friend Owner& get_property_owner(Property<Owner, Type, PropertyName_##Name>& p) { \
    return *reinterpret_cast<Owner*>(reinterpret_cast<uintptr_t>(&p) - offsetof(Owner, Name)); \
}

class WithProperties
{
public:
    // Explicitly define a property, begin.
    struct TagA {};

    Property<WithProperties, int, TagA> a;

    template<class T>
    friend WithProperties& get_property_owner(Property<WithProperties, T, WithProperties::TagA>& p) {
        return *reinterpret_cast<WithProperties*>(reinterpret_cast<uintptr_t>(&p) - offsetof(WithProperties, a));
    }

    void property_set(Property<WithProperties, int, TagA>& property_a, int value) {}
    int property_get(Property<WithProperties, int, TagA> const& property_a) const { return 'a'; }
    // Explicitly define a property, end.

    // Define a property using the convience macro, begin.
    PROPERTY(WithProperties, int, b);
    void property_set(Property<WithProperties, int, PropertyName_b>& property_b, int value) {}
    int property_get(Property<WithProperties, int, PropertyName_b> const& property_b) const { return 'b'; }
    // Define a property using the convience macro, end.
};

int main() {
    WithProperties x;
    x.a = 1;
    x.b = 2;
    int a = x.a;
    int b = x.b;
    static_cast<void>(a);
    static_cast<void>(b);
}