[英]Reference offset of member variable within POD declaration
Is it possible to have this code work somehow? 有可能让这个代码以某种方式工作吗? (Current get a compile error stating MMVertex2F4B2F does not exist) I don't want to wrap the 2 within another struct as that will effect the usability of the class within my existing code. (当前获得编译错误,声明MMVertex2F4B2F不存在)我不想将2包装在另一个结构中,因为这会影响我现有代码中类的可用性。
I need this all to happen at compile time, essentially the TemplatedClass needs to hold data regarding it's outer class that I can access later by creating an instance of TemplatedClass. 我需要在编译时发生这一切,本质上TemplatedClass需要保存有关它的外部类的数据,我可以稍后通过创建TemplatedClass实例来访问它。
struct MMVertex2F4B2F
{
MMPoint vertex;
MMColor4B col;
MMPoint tex;
struct TemplatedClass<offsetof(MMVertex2F4B2F, vertex)> {};
};
Regards, James 问候,詹姆斯
you could put the data of your main class into a sub-type: 您可以将主类的数据放入子类型:
struct MMVertex2F4B2F
{
struct MMVertexData {
MMPoint vertex;
MMColor4B col;
MMPoint tex;
} Data;
struct TemplatedClass<offsetof(MMVertexData, vertex)> {};
};
(if you access the data members via member methods, you only have to adapt those to this change instead of all of your code) (如果您通过成员方法访问数据成员,您只需要根据此更改而不是所有代码进行调整)
Alternatively, the TemplatedClass<>
could be declared standalone (not a sub-type) similar to a traits template. 或者,可以将TemplatedClass<>
声明为独立(不是子类型),类似于特征模板。
The fact that your code doesn't work shows that its design is flawed. 您的代码无效的事实表明其设计存在缺陷。
Note that 注意
struct TemplatedClass<offsetof(MMVertex2F4B2F, vertex)> {};
isn't valid C++ code. 是无效的C ++代码。 Either this is a template specialization, which would be 这是一个模板专业化,也就是说
template <> struct TemplatedClass<offsetof(MMVertex2F4B2F, vertex)> {};
(which doesn't seem like it would make much sense in this context) it's should be a declaration of an instance of the template, which would be (在这种情况下似乎没有多大意义)它应该是模板实例的声明,这将是
TemplatedClass<offsetof(MMVertex2F4B2F, vertex)> myInstanceVar;
For the rest, I agree with Walter's answer. 对于其他人,我同意沃尔特的回答。
The offsetof
macro is inherited from C, and it does work with POD structs, but it's not the best approach to referencing a member in C++. offsetof
宏继承自C,它确实可以与POD结构一起使用,但它不是在C ++中引用成员的最佳方法。 Parameterize the template on pointer-to-members. 在指向成员的指针上参数化模板。
template< typename client, MMPoint client ::* > // PTM parameter
struct TemplatedClass {
…
};
struct MMVertex2F4B2F
{
MMPoint vertex;
MMColor4B col;
MMPoint tex;
// PTM argument acceptable despite incomplete class:
TemplatedClass<MMVertex2F4B2F, & MMVertex2F4B2F::vertex> template_inst;
};
The template in this example accepts a pointer to member of type MMPoint
within any specified class. 此示例中的模板接受指向任何指定类中的MMPoint
类型成员的指针。 You might use some other parameterization. 您可以使用其他一些参数化。 Anything is better than byte offsets and casting through char *
. 任何东西都比字节偏移和通过char *
强制转换更好。
After sleeping on it I came to this solution which seems to work: 睡觉后,我来到这个似乎有效的解决方案:
struct MMVertex2F4B2F
{
MMPoint vertex;
MMColor4B col;
MMPoint tex;
struct TemplatedClass; //Use a forward declaration here
};
//The template will now work as MMVertex2F4B2F is fully declared
struct MMVertex2F4B2F::TemplatedClass
: public VertDef <offsetof(MMVertex2F4B2F, vertex)> {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.