[英]How to define a constant double at namespace scope with external linkage?
[英]What is the best way to define a double constant in a namespace?
在名称空间中定义双常量的最佳方法是什么? 例如
// constant.h
namespace constant {
static const double PI = 3.1415926535;
}
// No need in constant.cpp
这是最好的方法吗?
我会说:
-在c ++ 14中:
namespace constant
{
template <typename T = double>
constexpr T PI = T(3.1415926535897932385);
}
-在c ++ 11中:
namespace constant
{
constexpr double PI = 3.1415926535897932385;
}
-在c ++ 03中:
namespace constant
{
static const double PI = 3.1415926535897932385;
}
请注意,如果您的常量没有琐碎的类型,并且您位于共享库中,则我建议避免在全局/命名空间范围内为其提供内部链接,我不了解有关此原理的知识,但实际上它确实倾向于乱七八糟的东西:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.