简体   繁体   English

为什么 std::list 在 c++11 上更大?

[英]Why is std::list bigger on c++11?

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.我注意到在 GCC 4.7 上,C++98 上std::list<void*>的大小是 16 个字节,而在 C++11 上它的大小是 24 个字节。

I was wondering what changed on std::list that made it bigger.我想知道是什么改变了 std::list 让它变大了。

C++11 requires list::size() to execute in constant time. C++11 要求list::size()在恒定时间内执行。 GCC made this possible by adding the size as a data member . GCC 通过将大小添加为数据成员使这成为可能。 GCC did not do so for C++98 mode, because that would break binary compatibility. GCC 没有为 C++98 模式这样做,因为那样会破坏二进制兼容性。

Don't mix code compiled in C++98 mode with code compiled in C++11 mode.不要将以 C++98 模式编译的代码与以 C++11 模式编译的代码混合使用。 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.更新:显然,GCC 的人改变了主意,目前 C++11 的一致性不如保持兼容性重要,因此list::size()将不再在 GCC 4.7.2 中以恒定时间执行。 It will in a future version, in both C++98 and C++11 modes.它将在未来的版本中以 C++98 和 C++11 模式出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM