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

And*_*uel 27 c++ gcc c++11 c++98

使用此代码:

#include <iostream>
#include <list>

int main() {
    std::cout << sizeof(std::list<void*>) << std::endl;
};
Run Code Online (Sandbox Code Playgroud)

我注意到在GCC 4.7 std::list<void*>上,C++ 98 的大小是16字节,而它在C++ 11上的大小是24字节.

我想知道std :: list上有什么变化让它更大了.

小智 42

C++ 11需要list::size()在恒定时间内执行.GCC通过将大小添加为数据成员使这成为可能.GCC没有为C++ 98模式这样做,因为这会破坏二进制兼容性.

不要将在C++ 98模式下编译的代码与在C++ 11模式下编译的代码混合在一起.它不起作用.

更新:显然,海湾合作委员会的人改变了主意,而C++ 11的一致性现在不如维持兼容性那么重要,因此list::size()将不再在GCC 4.7.2中持续执行.它将在未来的版本中,在C++ 98和C++ 11模式下.

  • 现在,`std :: list <T> :: splice`的复杂性是多少? (2认同)