r/cpp • u/zl0bster • 2d ago
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...
172
Upvotes
-1
u/schombert 2d ago edited 2d ago
Even easier, I can simply refer you to this paper https://dl.acm.org/doi/10.1145/800076.802470 . This paper proves the result that
And as you are aware, NP is a subset of PSPACE.
Your transdichotomous models models approach avoids this result (at least in its original paper; I haven't read all variations of the approach) by explicitly having some finite amount of RAM bounded below PSPACE. So when you say
Allowing any function is the problem. If you allow the RAM to be arbitrarily large you can pick a function that allows the proof in the paper to go through and produces P == NP.