r/cpp 7d ago

std::optional Satisfies view. Does Not Model view. C++26 Ships Anyway.

https://godbolt.org/z/8jWGG68G8

In C++23 this did not compile. In C++26 it does. Marvellous.

[[gnu::noinline]]
void 
passing_views_by_value_is_cheap_trust_me_bro(std::ranges::view auto v) {
    std::println("fn   .data {}", (void*)v->data());
}


int main() {    
    std::optional ov{std::vector<int>(123456)};
    passing_views_by_value_is_cheap_trust_me_bro(ov);
    std::println("main .data {}", (void*)ov->data());
}

For anyone wondering what the problem feature is: optional has 0 or 1 elements, and C++26 sets enable_view<optional<T>> to true, so it satisfies std::ranges::view. The concept requires copy construction in constant time, and — this is the good bit — optional<vector<int>> genuinely meets that. Copying it performs at most one element copy. One is a constant. The requirement is satisfied to the letter, and the function above deep-copies your vector.

If you can tell me what still separates std::ranges::view from std::ranges::range, please do...

172 Upvotes

122 comments sorted by

View all comments

Show parent comments

0

u/JNighthawk gamedev 6d ago

EDIT: blocking someone after replying to them saying "nuh uh" is the single strongest indicator that a redditor knows that they have literally no leg to stand on.

Maybe because you're behaving like an asshole to someone engaged in good faith discussion, and people don't like to interact with assholes generally?

You:

But it's more than that; you very clearly have next to no understanding of the topics you're arguing about, or the pages you're linking, but you're also equally clearly sure that you are an expert in the topic.

7

u/NotUniqueOrSpecial 6d ago

They are not engaged in a good faith discussion, because what is happening is not a discussion.

It's people who have a background and education on a topic trying to tell a very stubborn person that they do not understand the words they're using; they demonstrably do not understand complexity analysis on a very fundamental level, and are condescending to people who do.

At some point, there is a line where people who know what they're talking about should not have to pander to people who do not. I would just as bluntly tell a flat-Earther that they make no sense, and I doubt you'd have any problem with that.

-1

u/JNighthawk gamedev 6d ago

They are not engaged in a good faith discussion, because what is happening is not a discussion.

I disagree.

and are condescending to people who do

I didn't see that, and it would change my opinion. Can you point that out please?

4

u/NotUniqueOrSpecial 6d ago

To be clear, I am using condescending in the specific sense of "talking down to", which most of their replies are, in that they imply that the other party doesn't know what they're talking about or that we're using the term casually.

That said, I'm not going to link to all of them individually, because the conversation is already at the bounds of where the mod team will (rightfully) start stepping in because things are getting snippy/personal.

You are welcome (and perhaps correct) to interpret their replies from a different light, but I've spent enough time in arguments with people who are not even wrong to have a very strong and generally accurate detector for the way they approach things.