条件constexpr函数
conditional constexpr functions
这里有一个函数可能是constexpr的例子。通常情况下,只有在上下文允许的情况下,才添加constexpr并使用常量求值。然而,尽管没有在constexpr上下文中使用它,但以下代码仍然存在问题:
template <typename T>
struct Wrapper
{
friend constexpr bool operator==(const Wrapper& crLhs, const Wrapper& crRhs) noexcept
{
return crLhs.m_t == crRhs.m_t;
}
T m_t = {};
};
使用Visual Studio 2017 15.9.20,例如为std::string实例化时,会出现"错误C3615:consexpr函数"运算符=="无法生成常量表达式"。信息是正确的,但我不是在constexpr上下文中实例化它。
void f()
{
bool b;
Wrapper<int> a;
b = a == a; //ok
Wrapper<std::string> c;
b = c == c; //C3615, but not using constexpr context
}
我可以通过使用成员模板或删除constexpr来应用一个变通方法,但这里有没有两全其美的妙招(即适用时的constexpr(?
您的代码很好。这是旧版本的MSVC中的一个错误
此错误已在MSVC 19.22版本中修复,编译时没有出现错误。在这里,我们可以看到两个编译器版本的并排:https://godbolt.org/z/79kXFm
19.22之后的所有版本(包括19.22(都会编译它,但19.21及以下的版本错误地给出了错误C3615,尽管它们都设置为使用C++11。
GCC和Clang从未出现过这个错误
这个错误只出现在MSVC中,即使是非常旧的GCC和Clang版本也会编译代码而不会出错。
你该怎么办
如果可能的话,您应该转移到Visual Studio的较新版本。这是升级编译器的最简单选项,如果您转到新版本,编译器应该会得到错误修复和升级。如果这不是一个选项,我会用谷歌搜索不同的方法来升级编译器本身。这可能会有所帮助。
相关文章:
- 条件constexpr函数
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- constexpr构造函数需要常量成员函数时出现问题
- constexpr 函数获取常量字符*
- 如何在 constexpr 函数中实现回退运行时
- 为什么我不能在 constexpr lambda 函数中使用 std::tuple
- 在非 constexpr 函数中作为左值传递的变量上使用 'constexpr' 函数
- 在 constexpr 构造函数 (c++17) 中赋值到 const char * 在使用 Android NDK 时
- NVCC 错误:string_view.h:constexpr 函数返回是非常量
- constexpr函数中的静态constexpr变量
- 在 constexpr funnction 中调用basic_string函数
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- constexpr log10 整数函数
- C++:初始化 constexpr 构造函数中的成员数组
- 编译器生成的默认构造函数具有 constexpr 混淆行为
- 在 constexpr 构造函数中初始化数组是否合法?
- 在 constexpr 函数中断言
- MSVC 和函数参数的 constexpr?
- 具有位域的结构的 Constexpr 构造函数
- C++ 默认构造函数 constexpr 或正确未定义