小编loa*_*er9的帖子

错误:表达式必须具有常量值。对于一个明显恒定的值

我有一个非常简单的项目,它在 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;
}
Run Code Online (Sandbox Code Playgroud)

positionVS2015 以红色突出显示构造函数中的参数。将鼠标悬停在突出显示的内容上时,会出现以下消息:

constexpr Vec2f position = {(0.0f), (0.0f)}

expression must have constant value
Run Code Online (Sandbox Code Playgroud)

与错误突出显示和消息相反,程序编译和运行顺利,构建日志中没有任何消息,警告级别为 4 且“将警告视为错误”处于活动状态。

让 VS 显示这样的错误并继续编译我的程序,甚至没有在构建日志中显示该消息......至少可以说是令人不安的。

为什么会显示此消息?
为什么它不停止应用程序的编译和运行?

嫌疑人

Vec2f的构造函数:

constexpr Vec2f::Vec2f(const float x, const float y) noexcept:
   x(x),
   y(y)
{
}
Run Code Online (Sandbox Code Playgroud)

物理组件构造函数:

constexpr PhysicsComponent::PhysicsComponent(Vec2f position, float mass, Vec2f momentum) noexcept:
   current(position, mass, momentum),
   previous(current),
   interpolated(current)
{
}
Run Code Online (Sandbox Code Playgroud)

“current”、“previous”和“interpolated”三个数据成员的类型为PhysicsState,其构造函数详述如下

constexpr PhysicsState::PhysicsState(Vec2f position, …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr visual-studio-2015

5
推荐指数
1
解决办法
2848
查看次数

标签 统计

c++ ×1

constexpr ×1

visual-studio-2015 ×1