【发布时间】:2016-08-11 07:48:24
【问题描述】:
何时使用constexpr,何时使用extern const?
我有这样的情况:
-
在标题(.h)中:
extern const int MAX_NUMBER_OF_ROWS; -
在源代码 (.cpp) 中:
const int MAX_NUMBER_OF_ROWS= 99;
文件(头文件和源文件)仅包含此类定义和声明。
是否建议只使用头文件中的constexpr 并去掉源文件,就像这里一样?:
// this is in the header file. There is no cpp file any more.
constexpr int MAX_NUMBER_OF_ROWS= 99;
【问题讨论】:
-
这些是正交的——你总是可以在标题中使用
const int MAX_NUMBER_OF_ROWS = 99;;然后问题是是否将const更改为constexpr。通常只有在包含标头时不知道该值的情况下才能按照您的方式使用它 -
好的,如果按照你说的在header里面,可以是
constexpr吧?
标签: c++ c++11 constants extern constexpr