简体   繁体   English

标准布局类的offsetof吗?

[英]offsetof on standard-layout class?

#include <iostream>
#include <cstddef>
class Foo
{
    int a;
    int b;
    float c;
};
int main()
{
    Foo foo;
    std::cout << offsetof(Foo, b) << std::endl;
    return 0;
}

The above code could not compile using gcc-4.8.2 or vc++11. 上面的代码无法使用gcc-4.8.2或vc ++ 11进行编译。 The error message is could not access private member b in class Foo. 错误消息是无法访问类Foo中的私有成员b。

But according to the standard, offsetof should support standard-layout class and Foo is a standard-layout class. 但是根据标准,offsetof应该支持标准布局类,而Foo是标准布局类。

Is this a defect of gcc-4.8.2 or vc++11, or my understanding of the c++ standard is wrong? 这是gcc-4.8.2或vc ++ 11的缺陷,还是我对c ++标准的理解是错误的?

Data members in a class are private by default . 默认情况下,类中的数据成员是私有的 they can be accessed from main only if it is declared under public .Or you can access them from main by declaring a function in the class under public and calling it from main. 只有在public声明了它们,才可以从main访问它们。或者,您可以通过在public下的类中声明一个函数并从main调用它,来从main访问它们。 since the functions in the class can access the data members from the class,you can get access to the private data members. 由于类中的函数可以访问类中的数据成员,因此您可以访问私有数据成员。

offsetof is defined as a macro and therefore it can not bypass the access controls and gain access to private members , we can see that this is the case by going to draft C++ standard section 17.6.1.2 Headers paragraph 5 which says ( emphasis mine ): offsetof被定义为一个 ,因此它不能绕过访问控制并获得对私有成员的访问权,我们可以通过起草C ++标准第17.6.1.2标题5段来说明这种情况( 强调我的意思 ):

Names which are defined as macros in C shall be defined as macros in the C++ standard library, even if C grants license for implementation as functions. 即使C授予了实现功能的许可,在C中定义为宏的名称也应在C ++标准库中定义为宏 [ Note: The names defined as macros in C include the following: assert, offsetof , setjmp, va_arg, va_end, and va_start. [注意:在C中定义为宏的名称包括:assert, offsetof ,setjmp,va_arg,va_end和va_start。 —end note ] —尾注]

Update 更新

So there are hacks that can allow you to access the private member of a class in a standard way but if we go back to the C99 draft standard which the draft C++ standard falls back on for offsetof then we see in section 7.17 Common definitions paragraph 3 says ( emphasis mine ): 因此,有一些骇客可以允许您以标准方式访问类的私有成员,但是如果我们回到C99草案标准(C ++标准草案依赖于offsetof),那么我们将在7.17节“ 通用定义”3段中看到说( 强调我的 ):

   offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). 它从其结构的开头(按类型指定)扩展为​​一个具有size_t类型的整数常量表达式,该值的大小为以字节为单位的结构成员(由mem​​ber-designator指定)的偏移量。 The type and member designator shall be such that given 类型和成员代号应使

   static type t;

then the expression &(t.member-designator) evaluates to an address constant . 然后表达式&(t.member-designator)的结果为地址常量

which won't be the case if you are trying to access a private member from outside the class. 如果您尝试从班级外部访问私人成员 ,情况就不会如此。

class members default to private. 类成员默认为私有。 List them as public, or make Foo a struct (because struct members default to public) and the above works, else main isnt permitted to access b, and offsetof is considered an access. 将它们列出为公共,或将Foo用作结构(因为结构成员默认为public),然后进行上述工作,否则main不允许访问b,而offsetof被视为访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 std::mutex 是标准布局类? - Why is std::mutex a standard-layout class? 对于 C++17 中的非标准布局类,“offsetof”“有条件地支持”是什么意思? - What does it mean for `offsetof` to be “conditionally-supported” for non standard-layout classes in C++17? 在标准布局对象(例如,使用 offsetof)中进行指针运算时,我们是否需要使用 std::launder ? - Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)? C ++中标准布局类的定义14 - Definition of standard-layout class in C++14 为什么这个结构不是标准布局? - Why is this struct not standard-layout? 标准布局和尾部填充 - Standard-layout and tail padding 联合本身是标准布局类型吗? - Is a union a standard-layout type itself? 现在,空基类优化是强制性优化(至少对于标准布局类而言)吗? - Is the Empty Base Class Optimization now a mandatory optimization (at least for standard-layout classes)? “黑匣子”类型的标准布局保证的目的是什么? - What is the purpose of standard-layout guarantees for “black box” types? 重新解释具有公共前缀的标准布局类型的转换指针 - Reinterpret casting pointers to standard-layout types with common prefixes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM