constexpr

    4熱度

    1回答

    我有4個測試案例,我相信,所有的人都是有效的: constexpr int f(int const& /*unused*/){ return 1; } void g(int const& p){ constexpr int a = f(p); // clang error, gcc valid int v = 0; constexpr int b =

    2熱度

    1回答

    此代碼編譯的GCC很好,但它會導致內部錯誤在Visual Studio 2017年編譯器?我的代碼有什麼問題?或者這是一個編譯器問題?我聽說VS沒有使用兩個階段名稱查找,這可能會在某些情況下導致問題。 template <typename T> constexpr auto doSomeSum(const T* a, const T* b) { if (a > b)return 0;

    2熱度

    1回答

    免責聲明:這個問題有點複雜,因爲它是一個問題,但它們都涉及到同一種概念/問題。 前提: consexpr功能可能僅由單個return聲明。 它們可以調用其他函數和使用條件,但理論上它們應該展示功能的純度,因此結果應該在某種映射中可以緩存(由編譯器在編譯時),這樣編譯器就沒有不斷重新評估相同的功能。 問題(S): 這是假設是正確的或者是有什麼我還沒有考慮,使得它不可能緩存constexpr函數的結果

    3熱度

    1回答

    我與static constexpr屬性有一些困難:它與積分類型一起使用,有enum class成員,但是當我嘗試使用靜態初始化的整數數組完成時,它失敗鏈接說undefined reference to S::a裏面main。 這是與鏗3.9或g ++ 6.3和ld 2.27.90;和所有與-std=c++14。 這裏是重現此最快的片段: struct S { static const

    2熱度

    2回答

    我試圖使用str_const類靈感來自https://stackoverflow.com/a/15863826/2859099 #include <cstddef> #include <stdexcept> class str_const { public: template<std::size_t N> constexpr str_const(const char(

    1熱度

    1回答

    header.h extern constexpr double sqrt_of_2; extern constexpr double sqrt_of_1_2; double sqrt(double x); 的main.cpp #include <header.h> int main() { int n; scanf("%d", &n); printf("

    1熱度

    2回答

    我想將一個更通用的const輸入參數傳遞給斐波納契的constexpr實現。當我用int替換模板參數時,事情又是一片混亂。 #include<iostream> template <typename T> constexpr auto fib_ce(T n) { return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1; } int main() {

    3熱度

    1回答

    我正在創建一個內核模塊。在這個模塊中,我需要根據一些預定義的字符串來檢查輸入。 在C++中,可以創建一個constexpr函數來計算編譯時的散列值。我正在尋找一種方式來做到這一點在C 使用the Jenkins hash function一些僞代碼: u32 hash(const char *key) { u32 hash, i; size_t len; len

    0熱度

    3回答

    我認爲這是不可能的什麼,我問,但我想是完全確定的,所以我問反正.. 我想從一個模板結構得到一個編譯時間值(可以在constexpr函數中使用),但是沒有在模板中傳遞,但以其他方式注入。 這是很難解釋,我會用一些代碼嘗試: template<int A> struct MagicStruct { enum { current = A, injected = /* magic */} };

    2熱度

    1回答

    我有一個C++ 14項目,我正在CLion 2016.3.4上開發,一段代碼給我檢查錯誤。我創建了一個最低限度的代碼來重現問題: #include <iostream> #include <type_traits> #include <system_error> using error_id_type = int; template <typename T> using enable_