本文介绍了使用static,const,constexpr的全局声明/初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++或C ++ 11中,对于以下声明//初始化,
In C++ or C++11, for the following declarations//initializations,
// 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
表示,在文件范围内,C ++中的第1行和第2行之间没有区别。
第三和第四行怎么样?
This question says at file scope there is no difference between line 1 and 2 in C++.How about line 3 and 4?
第4行和第5行之间有区别吗?
Are there differences between line 4 and 5?
第5行和第6行之间有区别吗?
Are there differences between line 5 and 6?
推荐答案
不,除了constexpr和const表示内部链接外,不应有任何区别(当然除了它们的值):
No, there should not be any difference (aside from their values of course) because constexpr and const implies internal linkage:
- ,显式声明为
static
的函数或函数模板;或者, - 明确声明的变量
const
或constexpr
和既未明确声明extern
,也未先前声明具有外部链接;或 - 匿名联合的数据成员。
- a variable, function or function template that is explicitly declared
static
; or, - a variable that is explicitly declared
const
orconstexpr
and neither explicitly declaredextern
nor previously declared to have external linkage; or - a data member of an anonymous union.
这篇关于使用static,const,constexpr的全局声明/初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!