Skip to content

To const or not to const? #1732

Answered by brevzin
ramblehead asked this question in Q&A
Aug 24, 2022 · 3 comments · 1 reply
Discussion options

You must be logged in to vote

Should const be used for views/ranges vars at all?

No. Not all views are const-iterable - some views are not because they have to logically mutate, others because they cache to satisfy complexity guarantees. Regardless, this means that by using const you make the variable not a range at all.

One example of a view that is sometimes not const-iterable is drop, which is why:

Is there a reason for for concat in (b) not accepting const view right?

Because if you write:

const auto left = slices | views::join;
const auto right = left | views::reverse | views::drop(1); /* (a) */

return /* (b){ */ views::concat(left, right) /* }(b) */ | ranges::to<std::u32string>();

then right is not actually…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ramblehead
Comment options

Answer selected by ramblehead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants