![](/img/trans.png)
[英]string literals as template arguments forces code duplication and verbosity
編譯時創建的字符串用作結構中的字符數組,其最大大小由該字符串驅動。 此外,該結構被用作另一個結構中的成員變量,並且其中有多個這樣的字符串緩沖區結構,這反過來又驅動了它的存儲大小。 使用字符串作為模板 arguments 確實有效,但引入了多個代碼重復,在源代碼中像野火一樣蔓延。 當第一個結構具有多個 ...
[英]string literals as template arguments forces code duplication and verbosity
編譯時創建的字符串用作結構中的字符數組,其最大大小由該字符串驅動。 此外,該結構被用作另一個結構中的成員變量,並且其中有多個這樣的字符串緩沖區結構,這反過來又驅動了它的存儲大小。 使用字符串作為模板 arguments 確實有效,但引入了多個代碼重復,在源代碼中像野火一樣蔓延。 當第一個結構具有多個 ...
[英]Examples of constinit declaration not reachable at the point of the initializing declaration
從dcl.constinit : 如果在初始化聲明處沒有可訪問的 constinit 聲明,則不需要診斷。 這是什么意思? 我想一個例子就足夠了。 動態初始化的東西只是格式錯誤(來自同一個鏈接),所以不是這樣。 ...
[英]Why does constinit allow UB?
假設我像這樣初始化變量: 由於整數溢出, b和c都會產生未定義的行為。 使用constinit變量被常量初始化。 這不能保證UB。 使用constexpr變量用常量表達式初始化。 常量表達式保證沒有任何 UB。 所以這里有符號整數溢出出現錯誤。 但該變量也自動為 const。 那么如何最好 ...
[英]Initialize a pointer with constinit
我想知道是否可以在 C++20 中使用 constinit 初始化一個指針,但我在互聯網上沒有找到任何適當的答案。 我有一個像這樣的簡單代碼: 並完美編譯。 但是,我看不到任何方法可以使其與 char * 以外的任何不同類型一起使用。 如果我想返回一個整數數組(其大小在編譯時已知)而不是“foo ...
[英]how to guarantee initilization of a stack variable with a compile time constant
在 C++20 中,我們現在有了constinit 。 constexpr和consteval 。 我現在可以保證 static 變量由constexpr或consteval function 的結果使用constinit進行初始化。 好的我還可以保證使用在編譯時執行的consteval func ...
[英]In which practical case is “const constinit” useful?
@Vittorio Romeo對這個問題的回答很好地解釋了constinit 。 在他的回答中,提到了以下幾點: constexpr不等同於const constinit ,因為前者要求不斷銷毀,而后者則不。 雖然很清楚,但我看不到它的任何實際用途。 在這種情況下將使用const co ...
[英]static_assert fails while using constinit const. Confusion in constinit, constinit const, constexpr, const, nonconst variables
我有一個關於編譯時函數的問題。 我知道 static_assert 應該只適用於可以在編譯時評估/計算的類型。 所以它不適用於 std::string (但是,gcc10 中不支持 constexpr std::string)但可以使用 std::array(當我在編譯時知道大小時)。 我正在觀看 ...
[英]When is the destructor of a constinit object called?
一般來說,static對象的析構函數的調用順序與構造函數的相反。 據我了解, constinit 對象是在編譯時初始化的,因此應該在“普通” static 對象的析構函數之后調用它們的析構函數。 該程序 (使用 GCC 10),但是,給出了 output 即 constinit object 在 ...
[英]Can C++20 `constinit` waive the need for nifty counter idiom?
C++20 引入了constinit以避免靜態初始化順序失敗。 constinit可以放棄對漂亮的計數器習慣用法的需要(例如,用於std::cout初始化)嗎? ...
[英]What is `constinit` in C++20?
constinit是 C++20 中的新關鍵字和說明符,它是在P1143中提出的。 標准中提供了以下示例: 想到幾個問題: constinit是什么意思? 為什么要介紹? 我們應該在哪些情況下使用它? 它是否使變量不可變? 它是指const還是constexpr ? 一個變量 ...