这个指针总是运行时构造吗

发布于 2025-01-25 01:57:53 字数 1511 浏览 2 评论 0原文

我正在学习 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
    }
    
};

demo

在上面的程序中,我认为我们可以使用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 of e, 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 of e;

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
    }
    
};

Demo

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2025-02-01 01:57:53

在核心常数表达式中使用此的唯一方法是调用构造函数(并在构造函数中使用)或在现有对象上调用成员函数。

此在constexpr函数或constexpr constructor 中,该<> 正在评估为e; ; ;


除constexpr函数或constexpr )

在您尝试时,常数表达式应为this-&gt; size(),但是person :: func(函数此出现在)未作为该表达的一部分进行评估。

这允许的简单示例:

struct S {
    int i = 0;
    constexpr S(int x) {
        this->i = x;
    }
    constexpr int get_i() const {
        return this->i;
    }
};

// `this` used in constructor
constexpr S s{7};
// `this` used in `S::get_i`
static_assert(s.get_i() == 7);
static_assert(S{4}.get_i() == 4);

演示: 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.

this except in a constexpr function or a constexpr constructor that is being evaluated as part of e;

(emphasis added)

In your attempt, the constant expression should be this->size(), but Person::func (the function this appears in) is not being evaluated as a part of that expression.

An simple example of what this allows:

struct S {
    int i = 0;
    constexpr S(int x) {
        this->i = x;
    }
    constexpr int get_i() const {
        return this->i;
    }
};

// `this` used in constructor
constexpr S s{7};
// `this` used in `S::get_i`
static_assert(s.get_i() == 7);
static_assert(S{4}.get_i() == 4);

Demo: https://godbolt.org/z/hea8cvcxW

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文