【发布时间】:2019-08-18 14:30:49
【问题描述】:
我有以下示例代码
template<class T1, class T2>
class Operation
{
public:
constexpr Operation(const T1& lhs, const T2& rhs) noexcept
: m_lhs(lhs), m_rhs(rhs) { }
private:
const T1& m_lhs;
const T2& m_rhs;
};
int main()
{
constexpr int a = 3;
constexpr int b = 4;
constexpr Operation op(a, b);
return 0;
}
用 cygwin (gcc 8.2) 编译我得到
error: 'Operation<int, int>{a, b}' is not a constant expression:
constexpr Operation op(a, b);
使用 MSVC 2019 可以正常编译,但 IntelliSense 讽刺地用工具提示“表达式必须具有恒定值”在 op(a, b) 中强调了 a。
关于问题是什么以及如何解决的任何建议?
【问题讨论】:
标签: c++ reference c++17 constexpr