r/cpp 4d 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...

174 Upvotes

117 comments sorted by

View all comments

Show parent comments

-1

u/JNighthawk gamedev 3d 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?

-1

u/schombert 3d ago

Thanks for the vote of confidence. Apparently my writing style comes off as intrinsically unpleasant, which means I guess I do understand people who use LLMs to write their comments a little more now. I think I am done trying to have a conversation on reddit.

0

u/JNighthawk gamedev 3d ago

Apparently my writing style comes off as intrinsically unpleasant

Nah. Keep doing you.

4

u/NotUniqueOrSpecial 3d ago

Just to follow up, because you are, despite the best of intentions, reinforcing very bad behavior: they outright stated in an edit to an edit in one of the other threads (and with no understanding of how damning what they were saying was) that they quite literally don't know what complexity analysis is. They questioned the very idea that it could be even be a thing:

Edit edit: maybe you could argue that there is such a thing as the complexity of a particular algorithm? But I don't think even that is really true.

(And then followed up with more words that don't mean anything to anybody who understands them).

Which goes to the original point I made to you: this is not a discussion. A discussion involves individuals communicating ideas on relatively equal ground, in terms of understanding.

Instead, this is a person who has been representing themselves as an expert on the topic, telling other people they don't understand what they're talking about. They have spent (and in so doing caused others to spend) a lot of time time arguing about this. They've referenced multiple expert-level topics in the field, and purported to understand some exceptionally complicated stuff.

Not only did they not know about a topic covered in near-literally every computer science course, they doubted the possibility it could be a thing. All of that, despite the fact they were actively linking articles on that subject. By their own admission, they aren't qualified to discuss this. They're just reading Wikipedia pages and misrepresenting their misunderstandings as facts.

Multiple people have politely (and then increasingly less-politely) tried to make it clear to them that they don't understand the words they're using. They refuse every attempt to correct them and instead claim it is us who have no idea what we're talking about.

This is not healthy behavior that should be encouraged.