至于我所做的研究,没有好的方法可以在静态上下文中获取封闭类的类型。非静态上下文呢?
例子:
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};
};
此代码有效,太棒了。但是尝试用 decltype(*this)
替换 MyType
并且它停止工作说这不能在静态上下文中声明。然而 my_prop
也不能被认为是静态上下文,它或多或少与 this 在相同的上下文中(可以改写为 this-> my_prop
)。
所以我的问题如下:我是不是遗漏了什么,或者在声明属性时即使在非静态上下文中也没有办法获取 this
的类型?
最佳答案
有点跑题了,但是如果你打算在 Owner
的每个属性成员中存储 Owner
指针,这在内存方面是次优的,即每个属性(property)成员都有一份完全相同的 Owner*
。
在 C++ 中,对象布局在编译时已知,因此,给定 Owner
成员的 this
指针,可以获得 Owner*
使用 offsetof
macro ,前提是成员名称已知。
仍然,要将数据成员名称注入(inject)到 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);
}
关于c++ - 在对象声明上下文中获取此类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764454/