You cannot add const
at any given level that easy. Different instantiations of a template are different unrelated types, a vector<T>
is unrelated to a vector<const T>
, and there is no way of casting from one to the other.
You can, on the other hand, create a different vector and just copy the contents, but that might be expensive, as you would have to copy all the different contained vectors.
By the way, if you return a const reference to the outer vector, the const reference will behave as: const std::vector< const std::vector< Foo * const > >&
, note that because of the value semantics associated to types in C++, const-ness propagates in. The problem is that the value stored in the inner vector is a pointer, and making that pointer constant does not make the pointed-to object constant. Similarly, the behavior of your getFoos(int)
will be equivalent to const std::vector< Foo * const >&
. Note, that is behavior not actual types.