标准(C ++)对编译时初始化有何看法?

时间:2021-08-28 03:11:09

In other words, when is a "static const" equivalent to a "#define"? I searched a lot and can't find anything. This is very relevant for global initialization dependencies (I know, globals are evil, but life is evil too!).

换句话说,什么时候“静态const”等同于“#define”?我搜索了很多,找不到任何东西。这与全局初始化依赖关系非常相关(我知道,全局变量是邪恶的,但生命也是邪恶的!)。

I am sure the standard does not force, but it probably mentions it.

我确信标准不会强制,但它可能会提到它。

E.g.:

static const int a = 2;  // Always seen it as compile time
static const int b = 2 * a;  // Compile time
static const float c = 3.14;  // Compile time
static const float d = c * 1.1 ;  // I know the evils of compile time here, but I've seen it both ways (compile and run time)
static const char e = 'p' ;  // Compile time
static const int *f = NULL;  // Never really tried it :)

3 个解决方案

#1


3  

This is specified by C++11 3.6.2: constant initialization is performed...

这是由C ++ 11 3.6.2指定的:执行常量初始化...

if an object with static or thread storage duration is not initialized by a constructor call and if every full-expression that appears in its initializer is a constant expression.

如果具有静态或线程存储持续时间的对象未通过构造函数调用初始化,并且其初始化程序中出现的每个完整表达式都是常量表达式。

This is your situation, except for d: int and float don't have constructors, and the initializer is a literal, which is a constant expression. Constant initialization is part of static initialization, and:

这是你的情况,除了d:int和float没有构造函数,初始化器是一个文字,它是一个常量表达式。常量初始化是静态初始化的一部分,并且:

Static initialization shall be performed before any dynamic initialization takes place.

在进行任何动态初始化之前,应执行静态初始化。

The standard doesn't explicitly prescribe an implementation, but there is no ordering of static initializations, and it is understood that the initial values are essentially provided at load time, and no code executes in order to get those global variables to have their initial values.

该标准没有明确规定实现,但没有静态初始化的顺序,并且可以理解初始值基本上是在加载时提供的,并且没有代码执行以使这些全局变量具有其初始值。

The variable d does not meet these criteria since its initializer is not a constant expression (because c is not a constant expression). The standard allows d to be initialized either dynamically or statically.

变量d不符合这些标准,因为它的初始值不是常量表达式(因为c不是常量表达式)。该标准允许动态或静态地初始化d。

#2


1  

Answering: 'In other words, when is a "static const" equivalent to a "#define"?'

回答:'换句话说,什么时候“静态const”等同于“#define”?

Never, the first is part of the programming language, the latter is a preprocessor text processing directive.

从来没有,第一个是编程语言的一部分,后者是预处理器文本处理指令。

#3


0  

Declaring static const vs a define are two different things. A value replaced by define can non-const and non-static.

声明静态const与定义是两回事。由define替换的值可以是非const和非静态的。

#MY_VAR 9
static const int a = MY_VAR;
int b = MY_VAR;

Defines essentially precompile time string replacement. Static and const keywords work all together different.

定义基本上是预编译时间字符串替换。 Static和const关键字的工作方式各不相同。

#1


3  

This is specified by C++11 3.6.2: constant initialization is performed...

这是由C ++ 11 3.6.2指定的:执行常量初始化...

if an object with static or thread storage duration is not initialized by a constructor call and if every full-expression that appears in its initializer is a constant expression.

如果具有静态或线程存储持续时间的对象未通过构造函数调用初始化,并且其初始化程序中出现的每个完整表达式都是常量表达式。

This is your situation, except for d: int and float don't have constructors, and the initializer is a literal, which is a constant expression. Constant initialization is part of static initialization, and:

这是你的情况,除了d:int和float没有构造函数,初始化器是一个文字,它是一个常量表达式。常量初始化是静态初始化的一部分,并且:

Static initialization shall be performed before any dynamic initialization takes place.

在进行任何动态初始化之前,应执行静态初始化。

The standard doesn't explicitly prescribe an implementation, but there is no ordering of static initializations, and it is understood that the initial values are essentially provided at load time, and no code executes in order to get those global variables to have their initial values.

该标准没有明确规定实现,但没有静态初始化的顺序,并且可以理解初始值基本上是在加载时提供的,并且没有代码执行以使这些全局变量具有其初始值。

The variable d does not meet these criteria since its initializer is not a constant expression (because c is not a constant expression). The standard allows d to be initialized either dynamically or statically.

变量d不符合这些标准,因为它的初始值不是常量表达式(因为c不是常量表达式)。该标准允许动态或静态地初始化d。

#2


1  

Answering: 'In other words, when is a "static const" equivalent to a "#define"?'

回答:'换句话说,什么时候“静态const”等同于“#define”?

Never, the first is part of the programming language, the latter is a preprocessor text processing directive.

从来没有,第一个是编程语言的一部分,后者是预处理器文本处理指令。

#3


0  

Declaring static const vs a define are two different things. A value replaced by define can non-const and non-static.

声明静态const与定义是两回事。由define替换的值可以是非const和非静态的。

#MY_VAR 9
static const int a = MY_VAR;
int b = MY_VAR;

Defines essentially precompile time string replacement. Static and const keywords work all together different.

定义基本上是预编译时间字符串替换。 Static和const关键字的工作方式各不相同。