Does C++11 allow vector?

前端 未结 4 736
不思量自难忘°
不思量自难忘° 2020-11-22 11:32

Container requirements have changed from C++03 to C++11. While C++03 had blanket requirements (e.g. copy constructibility and assignability for vector), C++11 defines fine-g

4条回答
  •  粉色の甜心
    2020-11-22 12:00

    Even though we already have very good answers on this, I decided to contribute with a more practical answer to show what can and what cannot be done.

    So this doesn't work:

    vector vec; 
    

    Just read the other answers to understand why. And, as you may have guessed, this won't work either:

    vector> vec;
    

    T is no longer const, but vector is holding shared_ptrs, not Ts.

    On the other hand, this does work:

    vector vec;
    vector vec;  // the same as above
    

    But in this case, const is the object being pointed to, not the pointer itself (which is what the vector stores). This would be equivalent to:

    vector> vec;
    

    Which is fine.

    But if we put const at the end of the expression, it now turns the pointer into a const, so the following won't compile:

    vector vec;
    

    A bit confusing, I agree, but you get used to it.

提交回复
热议问题