我试图初始化一个constexpr
参考没有成功.我试过了
#include <iostream>
constexpr int& f(int& x) // can define functions returning constexpr references
{
return x;
}
int main()
{
constexpr int x{20};
constexpr const int& z = x; // error here
}
Run Code Online (Sandbox Code Playgroud)
但我得到编译时错误
错误:constexpr变量'z'必须由常量表达式初始化
删除const
结果
错误:将类型'int'的引用绑定到类型'const int'的值会删除限定符
即使我有一种constexpr
自动暗示const
变量声明的感觉.
所以我的问题是:
constexpr
参考以往有用吗?(即比const
参考文献"更好" )PS:我看过几个与我有关的问题,例如哪些值可以分配给`constexpr`参考?,但我不认为他们解决了我的问题.