使用C+++ 23在编译器资源管理器上的23个功能期望

发布于 2025-02-09 07:00:37 字数 1745 浏览 2 评论 0原文

我在youtube上观看了一个C ++ con视频,发现在这里

我对这些新概念感兴趣。我尝试从幻灯片27和29的代码片段 @23:00- @26:30实现。我的代码有一个微妙的差异,在其中我将operator()()添加到my_function类中,以便在main中使用自动范围循环( )

另外,我必须在sort_by()函数中修改sell_than通过使用其operator()()()来调用函数>,以使编译器Explorer到编译代码。

这是我编译的代码的版本:

#include <iostream>
#include <vector>
#include <algorithm>

struct less_than {
    template<typename T, typename U>
    bool operator()(this less_than, const T& lhs, const U& rhs) {
        return lhs < rhs;
    }
};

struct my_vector : std::vector<int> {
    using std::vector<int>::vector;

    auto sorted_by(this my_vector self, auto comp) -> my_vector {
        std::sort(self.begin(), self.end(), comp);
        return self;
    }

    my_vector& operator()() {
        return *this;
    }
};

int main() {
    my_vector{3,1,4,1,5,9,2,6,5}.sorted_by(less_than());

    for (auto v : my_vector()) {
        std::cout << v << " ";
    }

    return 0;
}

这是我指向 Compiler Explorer 的链接编译的代码,汇编以及执行的输出。

该代码确实编译并执行。 main()中的向量确实包含其初始化器列表构造函数中可以看到的值。但是,似乎没有任何印刷到标准输出,或者是在C ++执行的同一条线中构造,分类然后销毁的,然后在自动范围循环中引用它之前,它不在范围内。

视频中此时的主题是关于自动推论该指针,以简化构建模式以降低crtp的复杂性,而这里的概念是引入by-value以下内容:移动链。

是的,这是实验性的,可能会在整个C ++ 23语言标准进行实施和发布之前发生变化。

我只是在寻找洞察力和清晰度,以确保根据编译器资源管理器,本演讲的主题以及新的语言功能的未来会带来什么,我了解我的代码中正在发生的事情。

我的假设是为什么我的输出不正确吗?它与对外的寿命,可见度和或出现范围有关吗?

I was watching a C++Con video on YouTube found here.

I became interested in these new concepts. I tried to implement the code snippets from slides 27 and 29 from the time stamps @23:00 - @26:30. There is a subtle difference in my code where I added the operator()() to my_function class in order to use it within the auto range loop within main().

Also, I had to modify the less_than within the sort_by() function call by using its operator()() in order for Compiler Explorer to compile the code.

Here is my version of the code that does compile:

#include <iostream>
#include <vector>
#include <algorithm>

struct less_than {
    template<typename T, typename U>
    bool operator()(this less_than, const T& lhs, const U& rhs) {
        return lhs < rhs;
    }
};

struct my_vector : std::vector<int> {
    using std::vector<int>::vector;

    auto sorted_by(this my_vector self, auto comp) -> my_vector {
        std::sort(self.begin(), self.end(), comp);
        return self;
    }

    my_vector& operator()() {
        return *this;
    }
};

int main() {
    my_vector{3,1,4,1,5,9,2,6,5}.sorted_by(less_than());

    for (auto v : my_vector()) {
        std::cout << v << " ";
    }

    return 0;
}

Here is my link to Compiler Explorer to see the actual compiled code, assembly as well as the executed output.

The code does compile and executes. The vector within main() does contain the values from its initializer list constructor that can be seen within the assembly. However it appears that nothing is being printed to the standard output, or it is being constructed, sorted and then destroyed from the same line of c++ execution and its going out of scope before referencing it within the auto range loop.

The topic at this point in the video is about automatically deducing the this pointer to simplify build patterns to reduce the complexity of CRTP and the concept here is to introduce By-Value this: Move Chains.

Yes this is experimental and may change before the entire C++23 language standard is implemented and released.

I'm just looking for both insight and clarity to make sure that I'm understanding what's happening within my code according to Compiler Explorer, the topic of this talk, and what the future may bring for newer language features.

Is my assumption of why I'm not getting an output correct? Does it pertain to object lifetime, visibility and or going out of scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你的呼吸 2025-02-16 07:00:37

我不能说我对C ++ 23了解很多,但我认为您的问题与Per-Se有关。这是:

for (auto v : my_vector()) ...

默认构建一个(空的,临时的)向量,然后在其上运行循环范围,MSVC显然足够聪明,可以看到这实际上是一个无op并将整个东西扔掉。

但是,如果您这样做:

for (auto v : my_vector{3,1,4,1,5,9,2,6,5}.sorted_by(less_than())) ...

那么,生成了合理代码的东西。 可惜我们无法运行它。稍微扩大了右侧的窗格,以查看程序输出!

godbolt链接

I can't say I know a lot about C++23, but I don't think your problem is to do with that per-se. This:

for (auto v : my_vector()) ...

default-constructs an (empty, temporary) vector and then runs a range for loop on it, and MSVC is evidently smart enough to see that this is effectively a no-op and throws the whole thing away.

But if you do this:

for (auto v : my_vector{3,1,4,1,5,9,2,6,5}.sorted_by(less_than())) ...

then what looks to me to be reasonable code is generated. Pity we can't run it. Widen the panes on the right-hand side a little bit to see the program output!

Godbolt link

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