How to make an immutable list in cpp? The list's reference should be constant as well as it's data.
I don't to make a wrapper class over vector or any other container (I know that method similar to this article).
What is the best way to do this using constexpr
or const pointers
.
Just declare it as
const
, like this:Methods like
constList.begin();
will return aconst_iterator
and calls likeconstList.push_back(3);
will not compile.Assigning its address to a non-const pointer won't work:
Passing a reference to a function that takes a non-const reference doesn't work:
Make a non-const list, and once you're done building it, move it to a
const
list: