在typedef而不是局部变量上使用sizeof

wef*_*fa3 14 c c++ typedef sizeof type-alias

就像在这个例子中(在C中):

typedef int type;

int main()
{
    char type;
    printf("sizeof(type) == %zu\n", sizeof(type)); // Outputs 1
}
Run Code Online (Sandbox Code Playgroud)

输出始终是局部变量的大小type.

当C++ struct在每次使用结构之前删除了写入的需要时,它仍然保留了struct {type}语法并引入了一个alias(class {type})来显式引用结构或类.

示例(在C++中):

struct type {
    int m;
};

int main()
{
    char type;
    printf("sizeof(type) == %u\n", sizeof(type)); // Outputs 1
    printf("sizeof(struct type) == %u\n", sizeof(struct type)); // Outputs 4
    printf("sizeof(class type) == %u\n", sizeof(class type)); // Outputs 4
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否有一种方法可以明确地引用typedefC或C++中的a.sizeof(typedef type)也许是喜欢的东西(但这不起作用).

我知道通常的做法是对变量和类型使用不同的命名约定以避免这种情况,但我仍然想知道是否有一种方法可以在langau中执行此操作或者如果没有.:)

Ven*_*esh 9

没有办法解决这个问题,但如果你的结构是全局定义的,你可以使用它,

范围解析运算符 ::.

printf("sizeof(type) == %zu\n", sizeof(::type));
Run Code Online (Sandbox Code Playgroud)