編譯器錯誤 C3615

constexpr 函式 ' function ' 不會導致常數運算式

函式 無法在編譯時期評估為 constexprconstexpr為 ,函式只能呼叫其他 constexpr 函式。

範例

當條件式評估作業的左側運算元在內容中 constexpr 無效時,Visual Studio 2017 會正確引發錯誤。 下列程式碼會在 Visual Studio 2015 中編譯,但不在 Visual Studio 2017 中編譯。

// C3615.cpp
// Compile with: /c

template<int N>
struct myarray
{
    int size() const { return N; }
};

constexpr bool f(const myarray<1> &arr)
{
    return arr.size() == 10 || arr.size() == 11; // C3615 starting in Visual Studio 2017
}

若要修正此問題,請將函 array::size() 式宣告為 constexpr 或從 f 中移除 constexpr 限定詞。