const in C vs const in C++

See the spec, in the compatibility appendix C.1.6: 7.1.6 [see also 3.5] Change: const objects must be initialized in C++ but can be left uninitialized in C Rationale: A const object cannot be assigned to so it must be initialized to hold a useful value. Effect on original feature: Deletion of semantically well-defined feature. Difficulty … Read more

vector and const

I’ve added a few lines to your code. That’s sufficient to make it clear why this is disallowed: void f(vector<const T*>& p) { static const T ct; p.push_back(&ct); // adds a const T* to nonConstVec ! } int main() { vector<T*> nonConstVec; f(nonConstVec); nonConstVec.back()->nonConstFunction(); }