Why is std::list bigger on c++11?

最后都变了- 提交于 2019-12-04 15:17:25

问题


with this code:

#include <iostream>
#include <list>

int main() {
    std::cout << sizeof(std::list<void*>) << std::endl;
};

I managed to notice that on GCC 4.7 the size of std::list<void*> on C++98 is 16 bytes, and its size on C++11 is 24 bytes.

I was wondering what changed on std::list that made it bigger.


回答1:


C++11 requires list::size() to execute in constant time. GCC made this possible by adding the size as a data member. GCC did not do so for C++98 mode, because that would break binary compatibility.

Don't mix code compiled in C++98 mode with code compiled in C++11 mode. It doesn't work.

Update: apparently, the GCC folks had a change of heart, and C++11 conformance is less important than maintaining compatibility for now, so list::size() will no longer execute in constant time in GCC 4.7.2. It will in a future version, in both C++98 and C++11 modes.



来源:https://stackoverflow.com/questions/10065055/why-is-stdlist-bigger-on-c11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!