What to expect when experimenting with C++23 features in MSVC on Compiler Explorer

822 Views Asked by At

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?

1

There are 1 best solutions below

9
Paul Sanders On

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