[英]What is the best way to define a double constant in a namespace?
What is the best way to define a double constant in a namespace? 在名称空间中定义双常量的最佳方法是什么? For example
例如
// constant.h
namespace constant {
static const double PI = 3.1415926535;
}
// No need in constant.cpp
Is this the best way? 这是最好的方法吗?
I'd say: 我会说:
-- In c++14: -在c ++ 14中:
namespace constant
{
template <typename T = double>
constexpr T PI = T(3.1415926535897932385);
}
-- In c++11: -在c ++ 11中:
namespace constant
{
constexpr double PI = 3.1415926535897932385;
}
-- In c++03 : -在c ++ 03中:
namespace constant
{
static const double PI = 3.1415926535897932385;
}
Note that if your constant does not have a trivial type and you are within a shared library, i would advise to avoid giving it internal linkage at global/namespace scope, i don't know the theory about this but in practice it does tend to randomly mess things up :) 请注意,如果您的常量没有琐碎的类型,并且您位于共享库中,则我建议避免在全局/命名空间范围内为其提供内部链接,我不了解有关此原理的知识,但实际上它确实倾向于乱七八糟的东西:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.