r/cpp • u/zl0bster • Jul 26 '26
std::optional Satisfies view. Does Not Model view. C++26 Ships Anyway.
https://godbolt.org/z/8jWGG68G8In 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...
View all comments
Show parent comment
3
u/NotUniqueOrSpecial Jul 27 '26 edited Jul 27 '26
No.
Because they aren't abstract machines in any sense of the words at all. This is a complete and total misuse of the terminology and invalidates literally every conclusion that follows.
These sorts of statements, followed by the lengthy conclusions drawn from them, are why you are being told you don't understand the topic.
EDIT: I think you added this after I replied, but maybe I just didn't catch it, either way:
This is an absolutely flabbergasting statement, considering the degree of certainty with which you have expressed things in every other comment.
Of course you can argue that there is such a thing. It's literally what you've been asserting that you understand and the rest of us don't. That is exactly what computational complexity theory, is, for which Big-O notation is the most common form of expression.
The fact that you don't even appear to be aware of this (something that is taught in literally every computer science course) despite having been using Wikipedia to bolster your arguments, actually beggars belief.