我试图使用一个指向int的const对象来初始化一个constexpr声明。同时,我还尝试使用一个非const类型的对象来定义一个对象。
代码:
#include <iostream>
int main()
{
constexpr int *np = nullptr; // np is a constant to int that points to null;
int j = 0;
constexpr int i = 42; // type of i is const int
constexpr const int *p = &i; // p is a constant pointer to the const int i;
constexpr int *p1 = &j; // p1 is a constant pointer to the int j;
}
g++ 日志:
constexpr.cc:8:27: error: ‘& i’ is not a constant expression
constexpr.cc:9:22: error: ‘& j’ is not a constant expression
我认为这是因为主函数中的对象没有固定的地址,所以g++会向我返回错误信息;我该如何纠正这个问题呢?不使用字面类型。
constexpr
关联起来。 - chris