cost 245 ms
在 C++ 中,如何為其中包含結構數組的結構創建 constexpr 聚合初始化?

[英]In C++, how do I create a constexpr aggregate initialization for a struct that has an array of structs in it?

我在 C++ 工作,我想為結構數組的結構創建一個初始化列表,但我一直遇到相同的編譯錯誤。 這是我的結構: 這是我的初始化列表: 我收到此錯誤: 我的初始化語法似乎非常正確...我不明白為什么會出現此錯誤。 ...

條件編譯(constexpr if)和“ISO C++ 禁止零大小數組”

[英]Conditional compilation (constexpr if) and "ISO C++ forbids zero-size array"

使用以下代碼: 我收到編譯器錯誤(帶有 -pedantic 標志),它抱怨不允許零大小數組。 在我的示例中,從未創建具有零大小數組的 object,但看起來它仍然是一個問題。 我試圖通過使用std::conditional來解決它,但即便如此,我還是得到了一個額外的 function,例如: 處理 ...

在必須連接兩個 const char* 變量的情況下,如何避免在 C++ 中使用 #define 宏?

[英]How to avoid using #define macros in C++ in a case where I have to concatenate two const char* variables?

我想在我的代碼中刪除對 #define 宏的依賴,但我無法使用constexpr實現相同的功能。 實際考慮以下示例: 我已經了解到fmt::format() function 不是一個constexpr function 而它只是一個運行時 function 的艱難方式。我期待我可以在代碼上更具表 ...

提供 const 和非常量版本的函數

[英]Providing const and non-const version of functions

也許這是一個微不足道的問題,但我實際上找不到答案。 如果我有一個如下例所示的 class,我是否需要像 std::vector 一樣提供常量和非常量版本的函數? 一個 constexpr function 會做同樣的工作嗎? ...

C++23 中的 constexpr function 有什么用?

[英]What is the use of a constexpr function in C++23?

關鍵字constexpr在將其引入 C++11 標准時對函數實施了非常嚴格的限制。 C++14 和 C++20(最值得注意的)放寬了這些限制: C++14 允許多個return語句、 static_assert等。 C++20 允許try和asm C++23 進一步放寬了這些限制。 從我在 ...

如何直接計算一系列數字並將它們存儲到 constexpr 變量而不在編譯時先計算它們的大小?

[英]How to directly calculate a series of numbers and store them to a constexpr variable without calculating their size first at compile time?

有沒有辦法避免get_primes_size ? 現在重復兩次計算小於 1000 的素數的過程。 就像先將它們推送到本地std::vector ,然后將其變成std::array一樣?constexpr bool is_prime(int n) { for (int i = 2; i * ...

為什么成員 function 不能要求同一個 class 的 static constexpr 成員為真?

[英]Why can't a member function require a static constexpr member of the same class to be true?

我嘗試讓一個成員 function 需要一個 static constexpr boolean 成員為真。 這對於 DRY 一個相當復雜的需求非常有幫助。 而且我不需要知道編譯器不允許我這樣做的原因。 要求稍微不那么復雜的最小示例: 這是因為在同一個isInt中聲明了 isInt 嗎? 還是我有某 ...

Constexpr 構造函數不在編譯時求值

[英]Constexpr constructor is not evaluated at compile time

我想在編譯時引入帶有錯誤檢查的強類型。 對於我的 chrono 類型,我注意到當底層類型從int64_t更改為int32_t時,字面量會悄無聲息地變窄,從而導致溢出。 所以我引入了一個顯式檢查。 但是,即使對於無法表示的常量參數(如delay_t {10s} ,編譯時也不會檢查此檢查。#includ ...

誰是 constexpr:編譯器還是程序員?

[英]Who is constexpr for: the compiler or the programmer?

使用 Compiler Explorer,我可以看到 GCC 可以在編譯時將稍微復雜的函數轉換為常量值。 例如: 只需將答案 216 插入到 main 的返回值中。 請注意,我在這里沒有使用關鍵字constexpr 。 編譯器自己解決了。 然而,根據我讀過的關於constexpr的一些web 頁, ...

將 constexpr char[] var 傳遞給 struct 的模板參數,然后使用 fmt 到 output 會拋出異常嗎?

[英]Pass a constexpr char[] var to template param of struct and then use fmt to output will throw an exception?

示例代碼 實際上,此代碼在 function 'basic_string_view(const Char* s)' 中拋出異常,我嘗試將 constexpr 替換為 const,然后一切正常。 有誰知道為什么? 我想知道為什么不能在這里使用 constexpr,這是規則還是錯誤? 更多信息: 操作 ...

如何定義一個 constexpr 構造的數組,它在許多翻譯單元上只有一個實例?

[英]How to define a constexpr constructed array that will have only one instance over many translation units?

我在constexpr class 內生成一個constexpr std::array ,但我只想要 class 的一個實例覆蓋它所使用的所有項目。 我最初將其設為全局變量,但后來我發現如果我在多個翻譯單元中遍歷數組,這些全局變量就會重復。 然后我嘗試使用constexpr function,但我 ...

在模板中使用 if constexpr

[英]Usage of if constexpr in template

我試圖了解 if constexpr 的效用,並想知道以這種方式使用它是否有任何效用。 這個 function 是否通過使用 if constexpr 而不是常規的 if 而改變了? 我假設性能是一樣的。 我對模板的理解是 if 語句的結果在編譯時已經知道,所以沒有區別。 ...

嘗試從 std::array compile.data() 作為 c++20 中的 constexpr function 時出錯

[英]error while trying to compile .data() from std::array as a constexpr function in c++20

我試圖在編譯時計算一個數組以加快某些函數的速度,但我遇到了一個錯誤,我無法在 cppreference 的幫助下解決該錯誤。 代碼歸結為: 在編譯時:“$ g++ -std=c++20 code.cpp” on ubuntu 我收到錯誤 that.data() is not a constexpr ...

如何將任何 constexpr function 作為參數傳遞給 constexpr 數組計算?

[英]How to pass any constexpr function as a parameter to constexpr array computation?

我很難將任何 constexpr function 作為參數傳遞給 constexpr 數組計算。 我將如何在常見的 C++ 中執行此操作(不是 constexpr,只是為了展示我想要的) 然而,在 constexpr 中並沒有那么簡單。 我從普通數組計算開始 關聯。 有效。 現在如何將 calc ...

我可以在編譯時使用 constexpr function 在數組上運行算法嗎?

[英]Can I use a constexpr function to run an algorithm on an array at compile time?

我有一個像下面這樣的數組: 我必須再添加 2 個字節來表示數組的校驗和。 校驗和計算如下: 最后,應將值x和y添加到末尾數組中。 我希望數組在static的方法中為 static。我不知道這是否重要。 如果我在數組中保留兩個額外的字節空間並在constexpr function 中通過引用傳遞它。 ...

從另一個 constexpr 中調用 Constexpr

[英]Constexpr call from within another constexpr

我知道,這些問題很早就被問到了(fe non-constexpr calls in constexpr functions ),但是......讓我們看下一個代碼: 一切正常。 我們保證, factorial(5)表達式在編譯時解析,因為consteval 。 正確的? 如果是這樣,我認為,這應該 ...

為什么我們不能在 Switch 中初始化一個“constexpr 變量”(在非最后一個 SWITCH CASE 下方)?

[英]Why can't we initialize a "constexpr variable" inside a Switch (below a non last SWITCH CASE)?

正如我們所知,我們不能在switch的任何case中初始化變量,除非它是相應switch的最后一個case ,因為變量的初始化確實需要定義在運行時執行(因為初始化程序的值必須是確定在這一點上), 但我們還知道constexpr變量將在編譯過程中被初始化或在代碼中被替換為它的值。 所以我嘗試了下面的 ...

使用 CRTP 調用靜態 constexpr 函數時出現未定義函數錯誤

[英]Undefined function error when calling a static constexpr function with CRTP

我試圖了解此示例中我的 CRTP 實現出現編譯錯誤的原因: 具體為什么 (A) 可以,而 (B) 給我這個錯誤: 將 (B) 替換為return impl_func() + 4編譯成功。 但是Derived::func()和Default<Derived>::func()都不起作用。 如 ...


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