简体   繁体   English

使用static,const,constexpr进行全局声明/初始化

[英]global declarations/initializations using static, const, constexpr

In C++ or C++11, for the following declarations//initializations, 在C ++或C ++ 11中,对于以下声明//初始化,

// global scope
const int a = 1; // line 1
static const int b = 2; // line 2
constexpr int c = 3;  // line 3
static constexpr int d = 4; // line 4
constexpr int e = a + b + c*d; // line 5
static constexpr int f = a - b - c*d; // line 6

This question says at file scope there is no difference between line 1 and 2 in C++. 这个问题说在文件范围内,C ++中第1行和第2行之间没有区别。 How about line 3 and 4? 3号线和4号线怎么样?

Are there differences between line 4 and 5? 4号线和5号线之间是否存在差异?

Are there differences between line 5 and 6? 5号线和6号线之间有区别吗?

No, there should not be any difference (aside from their values of course) because constexpr and const implies internal linkage: 不,不应该有任何区别(当然除了它们的值)因为constexpr和const意味着内部联系:

[C++11: 3.5/3]: A name having namespace scope (3.3.6) has internal linkage if it is the name of [C++11: 3.5/3]: 具有命名空间范围 (3.3.6)的名称具有 内部链接(如果它的名称)

  • a variable, function or function template that is explicitly declared static ; 显式声明为static的变量,函数或函数模板; or, 要么,
  • a variable that is explicitly declared const or constexpr and neither explicitly declared extern nor previously declared to have external linkage ; 一个显式声明为 constconstexpr 的变量, 既未显式声明为extern也未声明为具有外部链接 ; or 要么
  • a data member of an anonymous union. 匿名联盟的数据成员。

static variables exist for the lifetime of the program static is useful for functions like this: static变量存在于程序的生命周期中static对于这样的函数很有用:

void func (int i) {
    int var = i;
}

when a function is finish executing its code its objects destroys automatically to prevent this you can use static 当一个函数完成执行其代码时,它的对象会自动销毁,以防止你使用static

void func (int i) {
   static int var = i;
}

this mean that when a function finish executing its code the object defined as static will remain until the program ends 这意味着当函数完成执行其代码时,定义为static的对象将保留,直到程序结束

const applies for variables, and prevents them from being modified in your code. const适用于变量,并防止在代码中修改它们。 and constexpr are used for constant expressions this two is read-only it means that once you initialize a value it cannot be modified constexpr用于常量表达式这两个是只读的,这意味着一旦初始化一个值就无法修改

the difference of this two is: 这两者的区别在于:

static constexpr int d = 4; // line 4
constexpr int e = a + b + c*d; 

in static constexpr int d = 4 is we define a variable named d that is a static constant expression integer and have a value of 4 and cannot be modified and remain until the program ends static constexpr int d = 4中,我们定义一个名为d的变量,它是一个静态常量表达式integer,其值为4,不能修改并保留到程序结束

and in constexpr int e = a + b + c*d; constexpr int e = a + b + c*d; is we define a variable name e that is constant expression integer that have a value depends on what the result in those operations and cannot be modified 我们定义一个变量名e,它是一个常量表达式整数,其值取决于那些操作中的结果,并且不能被修改

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM