C++ 定义静态 const 结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/694706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:48:27  来源:igfitidea点击:

defining static const structs

c++csymbian

提问by dudico

This question is related to Symbian OS yet I think that C/C++ veteran can help me too. I'm compiling an open source library to Symbian OS. Using a GCCE compiler it compiles with no errors (after some tinkering :) ). I changed compiler to ARMV5 and now I have multiple errors with the definitions of static const structs, for example: I have a struct:

这个问题与 Symbian OS 有关,但我认为 C/C++ 老手也可以帮助我。我正在为 Symbian OS 编译一个开源库。使用 GCCE 编译器,它编译时没有错误(经过一些修补:))。我将编译器更改为 ARMV5,现在静态 const 结构的定义出现多个错误,例如:我有一个结构:

typedef struct Foos{
    int a;
    int b;
} Foos;

And the following definition of a const struct of type Foos

以及以下 Foos 类型的 const 结构的定义

static const Foos foo = {
    .a = 1,
    .b = 2,
};

GCCE has no problem with this one and ARMV5 goes "expected an expression" error on the ".a = 1, .b = 2,". From what I googled regarding this I reckon that this method should be legal in C but illegal in C++, if that's the case then what are the possibilities for declaring const structs in C++ ? If that's not the case then any other help will be appreciated.

GCCE 对此没有任何问题,ARMV5 在“.a = 1,.b = 2,”上出现“预期表达式”错误。根据我在谷歌上搜索的内容,我认为这种方法在 C 中应该是合法的,但在 C++ 中是非法的,如果是这种情况,那么在 C++ 中声明 const 结构的可能性是什么?如果情况并非如此,那么任何其他帮助将不胜感激。

Thanks in advance :)

提前致谢 :)

回答by Daniel Sloof

static const struct Foos foo = { 1, 2 };

Compiles with both g++ and gcc.

用 g++ 和 gcc 编译。

You could ofcourse, as onebyone points out, define a constructor:

你当然可以,正如 onebyone 指出的那样,定义一个构造函数:

typedef struct Foos {
    int a;
    int b;
    Foos(int a, int b) : a(a), b(b) {}
};

Which you would initalize like so:

你会像这样初始化:

static const struct Foos foo(1, 2);

回答by Steve Jessop

That's legal C99, but not legal C89 or C++. Presumably you're compiling this as C++, so if you use compiler options to enforce standards-compliance, then GCCE will reject it too.

这是合法的 C99,但不是合法的 C89 或 C++。大概你把它编译为 C++,所以如果你使用编译器选项来强制执行标准合规性,那么 GCCE 也会拒绝它。

You can do foo = {1, 2};in C or C++. Obviously you lose the benefit of the field names being right there: you have to rely on getting the order right.

你可以foo = {1, 2};用 C 或 C++来做。显然,您失去了字段名称就在那里的好处:您必须依赖正确的顺序。

Another good option in C++ is to define a constructor for your struct, and initialize with static const Foos foo(1,2);. This does prevent the struct being POD, however, so you can't make the same assumptions about its representation in memory.

C++ 中另一个不错的选择是为您的结构定义一个构造函数,并使用static const Foos foo(1,2);. 但是,这确实阻止了结构成为 POD,因此您不能对其在内存中的表示做出相同的假设。

回答by Shane Powell

Just a note that writable static datais not supported in DLL's on some versions of Symbian. It may not affect what you are doing as your examples are const or you may be only supporting Symbian v8.1b or later.

请注意,某些版本的 Symbian 上的 DLL 不支持可写静态数据。它可能不会影响您正在做的事情,因为您的示例是 const 的,或者您可能只支持 Symbian v8.1b 或更高版本。

I thought I would point it out as I fell into that trap once.

有一次我掉进了那个陷阱,我想我会指出来的。

Quote from the link above:

引用上面的链接:

Support for global writeable static data on Symbian OS

Symbian OS supports global writeable static data in EXEs on all versions and handsets.

Versions of Symbian OS based on the EKA2 kernel (8.1b and later) support WSD in DLLs on target hardware. Versions 8.1a and earlier, based on the EKA1 kernel, do not support global WSD in DLLs.

支持 Symbian OS 上的全局可写静态数据

Symbian OS 在所有版本和手机上都支持 EXE 中的全局可写静态数据。

基于 EKA2 内核(8.1b 和更高版本)的 Symbian OS 版本支持目标硬件上 DLL 中的 WSD。基于 EKA1 内核的 8.1a 及更早版本不支持 DLL 中的全局 WSD。

回答by Shane Powell

The dot style notation is I think valid in ANSI C99. It is not valid is ANSI C89. Almost all C compilers have not implemented C99.

我认为点样式符号在 ANSI C99 中有效。ANSI C89 无效。几乎所有的 C 编译器都没有实现 C99。