Proper handling of Lazy initialization needs language support that is currently unavailable in C++. But using the return value of functions is a possibility. Therefore, we can use the IILE(immediately invoked lambda expression) pattern:
auto const my_lazy_vec =
[& /*capture all by ref*/ ]{
std::vector<elements> nrvo;
{// safety scope for nrvo
if (case_1)
do_fill(nrvo, value_set_1);
else if (case_2)
do_fill(nrvo, value_set_2);
else if (case_3)
do_fill(nrvo, value_set_3);
return nrvo;
};//safety scope for nrvo
}(/*IILE*/);// init my_lazy_vec
In the above snippet nrvo is not essential, but serves as a good example. Using lambda, makes the initialization logic local and visible - while still conditional and flexible. And the result of immediate evaluation is directly initialized into a const object.
In other words, the mutable stage of object's lifetime is handled inside the lambda, while the immutable part is handled by the return value.
const std::vector<int>& getConstVector() { static std::vector<int> values = immediate_invoked_lambda[]{ ... initialization code here ... }(); return values; }const, you want logically constant. Then implement alazy<T>for your lazily initialized set of attributes. Make thelazy<T>have a logically constant interface, rather than be aconstobject itself.vectorcannot be modified in size it's contents can be since they cannot be const. see this