constexpr

    -2熱度

    1回答

    所以我知道在C++中使用constexpr是爲了在編譯時計算的定義表達式,更明顯是將變量或函數聲明爲常量表達式。我的困惑來自理解使用它的簡單功能的任何好處,這些功能不會改變。 假設你有一個功能簡單地廣場值... int square(int x) { return x * x; } 通常是永遠不會改變,或者被覆蓋,但是,我見過的人說,這將是更好的做法,而不是將其定義爲... con

    0熱度

    1回答

    參考cppreference's section on function templates: 函數模板或類模板的成員函數 不能使用inline或constexpr 的顯式實例 這些主題,inline和constexpr,似乎是分開和無關。爲什麼這個限制存在?

    0熱度

    2回答

    如果我有一個非成員constexpr函數取基準參數它必須是恆定的參考

    1熱度

    2回答

    考慮以下代碼: #include <array> template < int... Ints > constexpr std::array<int,sizeof...(Ints)> theIntArray = {Ints...}; template < size_t NN > constexpr void test(const std::array<int,NN>& xx) {

    5熱度

    1回答

    自從C++ 11以來,我們有constexpr函數,並且自從使用每個新標準(14,1z)以來,它們的限制就越來越少。 然而,在STL最明顯的功能,這可以由constexpr,該cmath/math.h功能,仍然沒有constexpr版本在任何標準庫實現AFAIK。 這是僅僅在C++標準的積壓,還是有其他原因,爲什麼我們仍然沒有constexpr版本的這些功能?

    3熱度

    1回答

    基於this問題,我嘗試了一個is_vector特點: #include <iostream> #include <vector> using namespace std; template<typename T> struct is_vector { constexpr static bool value = false; }; template<typename

    1熱度

    1回答

    我知道,有很多關於靜態(constexpr)成員鏈接的回答問題。 但我想知道,爲什麼在頭文件中使用模板類線外定義,但不適用於專門的類。 a)本工程不鏈接錯誤: template<typename, typename> struct Foobar; template<typename T> struct Foobar<int, T> { static constexpr std::

    6熱度

    4回答

    我希望能夠通過類型的ID創建switch語句。我發現了一種機制,可以爲不同類型提供唯一的ID。這很簡單: template <typename T> struct type { static void id() { } }; template <typename T> constexpr const size_t type_id() { return reinterp

    1熱度

    2回答

    我想知道是否可以通過參數包將簡單循環轉換爲constexpr並使用更簡單的代碼。此代碼示例演示什麼,我試圖做 struct Student { AgeCategory age; Income income; bool is_student; CreditRating credit_rating; bool buys_computer; };

    2熱度

    1回答

    Mike Isaacson在「Exploring C++ 17 and beyond」演示文稿中提到了一個問題(https://youtu.be/-ctgSbEfRxU?t=2907),有關於編寫的問題: const constexpr .... vs single const。 Mike說,在C++ 11中,constexpr暗含了const,而在C++ 14中則沒有。 這是真的嗎? 我試圖