gpt4 book ai didi

c++ - 变量如何既是 constexpr 又不是 constexpr?

转载 作者:可可西里 更新时间:2023-11-01 17:49:51 24 4
gpt4 key购买 nike

我做了一个constexpr字符串类型,我称之为StaticString。我从 this 得到这个想法网站。

我遇到了一些奇怪的问题,编译器在一行中将变量视为 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;

虽然标记为 constexpr 不是。因为在它的返回语句中:

return StaticString(str) + other;

由于创建了临时 StaticString(str) 指向非静态存储持续时间的临时指针。这是因为在您的 StaticString 对象中,您正在存储非静态存储持续时间临时对象的地址,并且常量表达式中不允许使用此类指针。

理由

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

A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints:

(5.1) — if the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression,

(5.2) — if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the address of a function, or a null pointer value, and

(5.3) — if the value is an object of class or array type, each subobject satisfies these constraints for the value.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.

.

关于c++ - 变量如何既是 constexpr 又不是 constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com