[英]Can I make a constexpr object of std::set?
我需要一個 std::set 的 const object,它將用於許多其他 cpp 文件。 由於應用程序每個部分的初始化順序未定義,因此當我使用此 std::set obj 初始化其他 const 對象時,我可能會得到一個空集。
所以,我想把這個 std::set 設置為 constexpr,但它不能被編譯。 我希望有:
constexpr std::set<int> EARLIER_SET = { 1, 2, 3 };
有沒有辦法得到它? 還是根本沒有?
根本不在標准庫中。
但您可能感興趣: https://github.com/serge-sans-paille/frozen
constexpr frozen::set<int, 3> EARLIER_SET = { 1, 2, 3 };
那么將是有效的。
你不能在這里使用constexpr
因為std::set
沒有constexpr
構造函數。
您可以做的是將變量聲明為inline const
變量,這將允許您將其包含在每個翻譯單元中並提供初始化程序。 那看起來像
//header file
inline const std::set<int> EARLIER_SET = { 1, 2, 3 };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.