本文介绍了为什么在c ++ 11上std :: list更大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

使用此代码:

  #include< iostream> 
#include< list>

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

我设法注意到,在GCC 4.7上, std :: list< ; C ++ 98上的void *> 是16字节,C ++ 11上的大小是24字节。不知道在std :: list上发生了什么变化,导致它变大。

解决方案

C ++ 11需要 list :: size()在常量时间内执行。 GCC通过来实现这一点。对于C ++ 98模式,GCC不这样做,因为这会破坏二进制兼容性。



不要将以C ++ 98模式编译的代码与编译的代码混合C ++ 11模式。它不起作用。



更新:显然,GCC人员的心态已经改变,而C ++ 11的一致性并不重要比现在保持兼容性,所以在GCC 4.7.2中, list :: size()将不再在常量时间内执行。它将在未来的版本中以C ++ 98和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.

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

解决方案

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.

这篇关于为什么在c ++ 11上std :: list更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:38