g++不会让我将模板参数传递给 offsetof
使用 g++ 时,我将模板参数作为成员变量传递给 offsetof,并收到以下警告:
invalid access to non-static data member 'SomeClass::t' of NULL object
(perhaps the 'offsetof' macro was used incorrectly)
这是我的用法:
template<typename T> class SomeClass { T t; };
...
offsetof(SomeClass, t); //warning: invalid access to non-static data member 'SomeClass::t' of NULL object, (perhaps the 'offsetof' macro was used incorrectly)
使用 __builtin_offsetof 时出现相同的错误。有什么想法吗?
谢谢
When using g++ I pass a template parameter as the member variable to offsetof, and I get the following warning:
invalid access to non-static data member 'SomeClass::t' of NULL object
(perhaps the 'offsetof' macro was used incorrectly)
Here is what my usage looks like:
template<typename T> class SomeClass { T t; };
...
offsetof(SomeClass, t); //warning: invalid access to non-static data member 'SomeClass::t' of NULL object, (perhaps the 'offsetof' macro was used incorrectly)
I get the same error using __builtin_offsetof. Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
成员数据必须是公共的,因此使用 public 或 struct
请注意,预处理器总是尝试以逗号分隔参数,因此使用 typedef 作为解决方法。
编辑:抱歉,我误解了你的问题,所以我更改了答案 + lt & GT
Member data must be public, so use public or struct
Note that preprocessor alway try to split arguments at a comma, so use a typedef as a workaround.
edit: sorry, I misunderstood your question, so I have changed the answer + lt & gt
这里有同样的问题,offsetof 不适用于模板类。
作为解决此问题的快速技巧,只需创建该类型的虚拟对象,并通过减去地址来计算偏移量:
Had the same problem here, offsetof doesn't work with templated classes.
As a quick hack to solve this, just create a dummy object of that type, and calculate the offset by subtracting adresses: