1
我這是在VS2015產生奇怪的行爲非常簡單的項目:錯誤:表達式必須具有常數值。爲了明確常數值
#include "Vec2f.h"
#include "StaticRendercomponent.h"
int main(int argc, char** argv)
{
constexpr Vec2f position(0.0f, 0.0f);
constexpr PhysicsComponent component(position, 15.0f);
return 0;
}
VS2015是着重於紅色的構造函數中的參數position
。 當鼠標懸停在所述高亮顯示,顯示以下消息:
constexpr Vec2f position = {(0.0f), (0.0f)}
expression must have constant value
相反對錯誤高亮和消息,該程序編譯和順利並且不會生成日誌的任何消息中運行,以警告級別4和「將警告視爲錯誤「處於活動狀態。
讓VS顯示這樣的錯誤並繼續編譯我的程序,甚至沒有在構建日誌中顯示消息是......讓人不安的是,至少可以說。
爲什麼顯示此消息?
爲什麼它不停止編譯和運行應用程序?
嫌疑人
Vec2f的構造函數:
constexpr Vec2f::Vec2f(const float x, const float y) noexcept:
x(x),
y(y)
{
}
的PhysicsComponent構造:
constexpr PhysicsComponent::PhysicsComponent(Vec2f position, float mass, Vec2f momentum) noexcept:
current(position, mass, momentum),
previous(current),
interpolated(current)
{
}
的三個數據成員 「當前」, 「以前」 和 「插值」 是的類型PhysicsState,其構造函數詳述如下
constexpr PhysicsState::PhysicsState(Vec2f position, float mass, Vec2f momentum) noexcept:
mass(mass),
inverseMass(MathUtils::computeInverse(mass)),
position(position),
momentum(momentum),
externalForce(ZERO_VECTOR)
{
}
computeInverse的簽名如下:
template<typename T>
constexpr T computeInverse(T value) noexcept
我還是想知道,如果PhysicsStates沒有成功被constexpr由編譯器初始化由於呼叫computerInverse()
,但添加一行:
constexpr PhysicsState test(position, 15.0f);
到main()
編譯並運行時沒有消息,也沒有突出顯示。
對此問題的任何深入瞭解將不勝感激。
智能感知遠非完美。從來沒有,也很可能永遠不會。當您收到混合消息時,請相信編譯器。 – molbdnilo
啊,我想不會像我期望的那樣有趣......答案似乎是最簡單和可能的解釋,我忽略了這個問題。 – loaner9
IntelliSense是編譯器的*模擬*。這往往是錯誤的(正如你所看到的)。忽略它。 – rustyx