这个指针总是运行时构造吗
我正在学习此
C ++中的指针。然后我遇到以下“ noreferrer”> stragent 来自标准:
表达式
e
是核心常数表达式>
- 的一部分进行评估。
此
,除了constexpr函数或constexpr构造函数以e
;
我不了解上述语句的含义。我认为此
指针是运行时构建,因此不能在编译时上下文中使用。但是上述陈述似乎在上述情况下暗示了其他方式。 我的第一个问题是这是什么意思。有人可以举个例子,以便我可以理解该陈述的含义。
我试图创建一个示例以更好地理解我自己的陈述含义,但该程序无效正如预期的那样(这意味着在我认为应该根据引用的语句工作时会出现错误):
struct Person
{
constexpr int size()
{
return 4;
}
void func()
{
int arr[this->size()]; //here size is a constexpr member function and so "this" should be a core constant expression and arr should not be VLA
}
};
在上面的程序中,我认为我们可以使用expression this-> size()
作为数组的大小(必须是编译时间常数),因为> size
是 constexpr ,因此引用的语句适用,因此该程序应编译。另外,由于size
是 constexpr arr
不应是VLA。但是令我惊讶的是,arr
似乎是VLA,并且该程序也没有在MSVC中编译(因为我认为MSVC没有VLA)。因此,我的第二个问题是为什么不适用上述示例中的引用语句,为什么arr
vla?我的意思是size
是constexpr,所以arr
不应该是VLA。
I am learning about the this
pointer in C++. And i came across the following statement from the standard:
An expression
e
is a core constant expression unless the evaluation ofe
, following the rules of the abstract machine, would evaluate one of the following expressions:
this
, except in a constexpr function or a constexpr constructor that is being evaluated as part ofe
;
I do not understand the meaning of the above quoted statement. I thought that the this
pointer is a runtime construct and so it cannot be used in compile-time contexts. But the above statement seems to suggest otherwise in the mentioned contexts. My first question is what does it mean. Can someone give some example so that i can understand the meaning of that statement.
I tried to create an example to better understand the meaning of the statement by myself but the program did not work as expected(meaning that it gave error while i thought that it should work according to the quoted statement):
struct Person
{
constexpr int size()
{
return 4;
}
void func()
{
int arr[this->size()]; //here size is a constexpr member function and so "this" should be a core constant expression and arr should not be VLA
}
};
In the above program, i thought that we're allowed to use the expression this->size()
as the size of an array(which must be a compile time constant) because size
is constexpr and so the quoted statement applies and so the program should compile. Also, since size
is constexpr arr
should not be VLA. But to my surprise, the arr
seems to be VLA and the program also doesn't compile in msvc(because msvc don't have vla i think). So my second question is why doesn't the quoted statement applicable in the above example and why is arr
VLA? I mean size
is constexpr so arr
shouldn't be VLA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在核心常数表达式中使用此的唯一方法是调用构造函数(并在构造函数中使用)或在现有对象上调用成员函数。
此
除constexpr函数或constexpr )在您尝试时,常数表达式应为
this-> size()
,但是person :: func
(函数此出现在)未作为该表达的一部分进行评估。这允许的简单示例:
演示: https://godbolt.org.org/z/hea8cvcxw
The only way to use
this
in a core constant expression is to call a constructor (and use it in the constructor) or call a member function on an existing object.(emphasis added)
In your attempt, the constant expression should be
this->size()
, butPerson::func
(the functionthis
appears in) is not being evaluated as a part of that expression.An simple example of what this allows:
Demo: https://godbolt.org/z/hea8cvcxW