一个变量怎么可能既是constexpr又不是constexpr

How can a variable be both constexpr and not constexpr?

本文关键字:constexpr 怎么可能 变量 一个      更新时间:2023-10-16

我创建了一个constexpr字符串类型,称之为StaticString。我是从这个网站上得到这个想法的。

我遇到了一些奇怪的问题,编译器在一行中将变量视为constexpr,然后在下一行将变量视为不属于constexpr

这是代码:

constexpr StaticString hello = "hello";
constexpr StaticString hello2 = hello + " ";
constexpr StaticString world = "world";
constexpr StaticString both = hello + " world";
constexpr StaticString both2 = hello2 + world;
//This works fine (world is constexpr?)
//constexpr StaticString both3 = "hello " + world; 
//ERROR: "world" is not constexpr
int main(void)
{
static_assert(hello[4] == 'o' ,"ERROR");
static_assert(hello == "hello", "ERROR");
static_assert(both2 == "hello world", "ERROR");
}

这是StaticString:的定义

class StaticString{
const char* const str;
const size_t len;
const StaticString* head;
public:
template<size_t N>
constexpr StaticString(const char(&aStr)[N]) 
: str(aStr), len(N-1), head(nullptr)  //Chop off the null terminating char
{
static_assert(N>=1,"String cannot have a negative length");
}
template<size_t N>
constexpr StaticString(const char(&aStr)[N] ,const StaticString* ss) : head(ss), str(aStr),len(N-1) { }
constexpr StaticString(const char* const aStr ,const size_t len,const StaticString* ss = nullptr) 
: str(aStr), len(len), head(ss)
{
}
constexpr char GetFromHead(size_t index) const{
return index < head->GetSize() ? (*head)[index] : str[index - head->GetSize()];
}
constexpr char operator[](size_t index) const{
return head ? GetFromHead(index) : str[index];
}
constexpr size_t GetSize() const{
return head ? len + head->GetSize() : len;
}
constexpr bool Equals(const char* const other,size_t len,size_t index = 0) const{
return (other[0] == (*this)[index]) ? (len > 1 ? Equals(&other[1],len-1,index+1) : true) : false;
}
template<size_t N>
constexpr bool operator==(const char(&other)[N]) const{
return Equals(other,N-1);
}
template<size_t N>
constexpr StaticString operator+(const char(&other)[N]) const{
return StaticString(other,this);
}
constexpr StaticString operator+(StaticString other) const{
return StaticString(other.str,other.len,this);
}
};
template<size_t N>
constexpr StaticString operator+(const char(&str)[N],const StaticString& other){
return StaticString(str) + other;
}

所以我的问题是:为什么world在一行被视为constexpr,而在下一行却没有?

注意:这是我得到的错误:

'StaticString{((const char*)"world"), 5ull, ((const prototypeInd::util::StaticString*)(&<anonymous>))}' is not a constant expression

此外,我正在使用gcc

讨论

您的world变量是constexpr,但表达式中的operator+

constexpr StaticString both3 = "hello " + world;

尽管标记为CCD_ 12不是。因为在其返回声明中:

return StaticString(str) + other;

由于创建了指向非静态存储持续时间临时存储器的临时CCD_ 13指针。这是由于在StaticString对象中存储非静态存储持续时间临时变量的地址,而常量表达式中不允许使用此类指针。

正当性

根据标准§5.20/p5常量表达式[expr.const](Emphasis Mine):

常量表达式要么是glvalue核心常量表达式其值指的是一个实体,该实体是常量表达式(定义如下),或prvalue核心常量其值满足以下约束的表达式:

(5.1)--如果值是类类型的对象,则每个非静态数据引用类型的成员指的是允许的实体常数表达式的结果

(5.2)--如果值为指针类型,则它包含具有静态存储持续时间的对象,超过该持续时间末尾的地址对象(5.7)、函数地址或空指针值,和

(5.3)--如果值是类或数组类型的对象,则每个子对象满足这些值约束

如果实体是常量表达式的允许结果,则它是对象的静态存储持续时间不是临时的对象或是值满足上述条件的临时对象约束,或者它是一个函数。