c++ - constexpr 引用有什么用吗?

标签 c++ c++11 reference constexpr

正如标题所说,constexpr引用有什么有用的case吗?像这样:

static const int myvar = myfunc();  // myfunc is not constexpr
constexpr const int& myref = myvar;

myref 无论如何都不能用在需要常量表达式的地方,因为它引用的变量不是常量表达式。我看不出这与以下内容有何不同:

static const int myvar = myfunc();
const int& myref = myvar;

生成的程序集看起来也是一样的。

最佳答案

constexpr& 在程序启动之前被初始化,并且必须绑定(bind)到具有静态存储持续时间的变量,这对于 const& 来说都不是必需的。

关于c++ - constexpr 引用有什么用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672395/

相关文章:

c++ - 如何在目录中以特定格式获取行的特定部分?

c++ - 这个异步技巧会起作用还是当我访问它时状态会悬空?

c++ - C++ 编译器可以假定 const bool & value 不会改变吗?

c++ - 用于新运算符分配的公共(public)内存

c++ - 映射迭代器对值的复制省略

c# - 拆分项目 VB/C# 和引用

python - 如何通过引用传递变量?

c++ - 这是 C++ 中什么类型的错误?

c++ - 没有调用 'this was nullptr"时如何修复 "this"

c++ - 为什么显式运算符 std::string 不起作用