簡體   English   中英

在名稱空間中定義雙常量的最佳方法是什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM