- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在 MinGW GCC 4.4.0 中编译一些 c++ 代码,并收到以下形式的警告...
warning: invalid access to non-static data member '<membername>' of NULL object
warning: (perhaps the 'offsetof' macro was used incorrectly)
这个问题似乎很熟悉——我之前尝试过解决但失败了,我想,但不久前。该代码在 Visual C++ 中构建良好,但我最近没有在任何其他编译器中构建此特定代码。
问题代码如下模板...
template<typename T>
class c_Align_Of
{
private:
struct c_Test
{
char m_Char;
T m_Test;
};
public:
enum { e_Align = offsetof (c_Test, m_Test) };
};
显然我可以使用一些条件编译来为此使用编译器特定的函数,并且我相信 C++0x 将(最终)使其变得多余。但无论如何,我看不出这种使用 offsetof
有什么问题。
非常迂腐,有可能因为 T
参数类型有时是非 POD,所以 GCC 将 c_Test
归类为非 POD 和提示(并且提示和提示 - 我收到了近 800 行这些警告)。
这对于标准的严格措辞来说是很顽皮的,因为非 POD 类型可能会破坏 offsetof
。但是,这种非 POD 在实践中应该不是问题 - c_Test
不会有虚拟表,并且不需要运行时技巧来找到 m_Test< 的偏移量
。
此外,即使 c_Test
有一个虚拟表,GCC 也使用一个内在函数来实现 offsetof 宏,该内在函数总是在编译时根据该特定类型的静态布局进行评估。提供一个工具,然后每次使用它时都会提示(抱歉,警告)看起来很愚蠢。
而且,我不是这里唯一一个做这种事情的人......
Answer to legit-uses-of-offsetof question
我确实记得由于这种原因,offsetof
有问题,但我不认为问题出在这个模板上。
有什么想法吗?
最佳答案
哎呀...
问题 是,由于 T 类型是非 POD,因此 c_Test
结构是非 POD。这是来自 GCC 手册的引述...
-Wno-invalid-offsetof (C++ and Objective-C++ only)
Suppress warnings from applying the ‘offsetof’ macro to a non-POD type.
According to the 1998 ISO C++ standard, applying ‘offsetof’ to a non-POD type is undefined. In existing C++ implementations, however, ‘offsetof’ typically gives meaningful results even when applied to certain kinds of non-POD types. (Such as a simple ‘struct’ that fails to be a POD type only by virtue of having a constructor.) This flag is for users who are aware that they are writing nonportable code and who have deliberately chosen to ignore the warning about it.
The restrictions on ‘offsetof’ may be relaxed in a future version of the C++ standard.
我的问题是我几乎所有的 T 类型都有构造函数,因此被归类为非 POD。我之前忽略了这一点,因为它无关紧要——当然,原则上它应该与 offsetof 无关。问题是 C++ 标准使用一种 POD 与非 POD 分类,即使有许多不同的方式可以成为非 POD,并且编译器在默认情况下警告不符合标准的使用是正确的。
我目前的解决方案是上面的选项来抑制警告 - 现在我只需要弄清楚如何告诉 cmake 使用它。
关于c++ - 使用 offsetof 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3129916/
使用 OFFSETOF 宏更新结构字段的代码 #include #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
刚看到What does the following macro do?我要问我自己的问题:为什么这么多应用程序的 header 定义 offsetof他们自己? 有什么原因吗?不靠谱? 最佳答案
来自 C++ 标准: A standard-layout class is a class that: — has no non-static data members of type non-sta
我想知道一个简单的宏 offset_of_ 是否需要对 not 的指针取消引用。例如,一个 C++(意味着这段代码将使用 C++ 编译器编译)结构,它声明了 packed 属性 struct A {
我想要 mystruct1 中的 offsetof() 参数行。我试过了 offsetof(struct mystruct1, rec.structPtr1.u_line.line) 还有 offse
就在这个问题因重复而被驳回之前,大多数(如果不是全部)问题都已接受答案,并带有过时/未定义的行为解决方案。 问题: 有没有办法在编译时获得指向成员数据的指针的偏移量: 不依赖于未定义的行为(nullp
我编写了一个小函数来获取结构内部字段的字节偏移量,但为了实现这一点,我每次都会迭代这些字段,直到找到符号。然而,在 C 中,offsetof() 仅在编译时计算一次,因为结构内部的偏移量不再改变。我想
当我这样定义结构时: struct abc{ int member1; int arr_member1; std::vector mVector; } 并且想要创建一个 arr_member 变
我必须在已经存在的大代码中添加一段代码。我的代码中有一个指针,*Ptr。该指针的类型为 unsigned char。 无符号字符 *Ptr; 我有以下结构。 struct { uint8_t t
相关文章: [c 循环双链表:rev 遍历为同一节点提供不同的列表指针地址](c circular double linked-list: rev traverse gives different l
这个问题已经有答案了: 已关闭11 年前。 社区去年审查了是否重新开放此问题,并将其关闭: 原始关闭原因未解决 Possible Duplicate: Why does this C code wor
根据我的另一个问题,这是一个我想避免 offsetof 的具体示例。 为了与 glVertexAttribPointer 一起使用,我必须对最后一个参数使用 offsetof。 glVertexAt
我想在这里实现“GetParent()”函数- class ChildClass; class ParentClass { public: .... ChildClass childO
我看到了offsetof这两个宏: #if defined(_MSC_VER) && !defined(_CRT_USE_BUILTIN_OFFSETOF) #ifdef __cplusplus
我有以下代码,以便能够访问结构数组中的多个字段(为简单起见,我已将其减少为两个)。最终指针计算的正确咒语是什么 *(ptr + offset) = 数据;因为我总是得到: 错误:从类型“int32_t
我一直在尝试使用宏连接 (##) 和 offsetof 宏来生成标识符,但没有成功: CMain.c:55: warning: implicit declaration of function 'Co
我正在尝试以下列方式使用 offsetof 宏: typedef unsigned char u8; typedef unsigned short u16; struct MapBlock { u
我正在尝试在我自己的另一个宏中使用 offsetof 宏,如下所示: #define MY_MACRO(struct_type, member) \ my_function(param1, o
这个问题在这里已经有了答案: Why does this implementation of offsetof() work? (8 个答案) 关闭 3 年前。 offsetof 在stddef.h
我试图通过使用 offsetof() 宏来学习如何访问结构成员,我遇到了这个例子 How can I access structure fields by name at run time?这一行:
我是一名优秀的程序员,十分优秀!