Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

c++ - Should std::list::size have constant complexity in C++11?

I am using gcc 4.8.1 and after hours of debugging a horrible mysterious performance issue I found out that the std::list::size is actually implemented as a call to std::distance.

/**  Returns the number of elements in the %list.  */
      size_type
      size() const _GLIBCXX_NOEXCEPT
      { return std::distance(begin(), end()); }

This surprised me, since the reference says that the complexity of std::list::size should be constant and the complexity of std::distance is linear for std::list::iterator.

I am really confused, since I think gcc has excellent support for C++11 features and I see no reason why they would not implement this one.

Is this an error in the reference or in gcc?

In the latter case:

is there any reason why such a fundamental C++11 feature would be missing for so long?

Is there a third possibility e.g.:

Could I have gcc 4.8.1 but some older version of the standard library?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is not exactly a bug and you can read about it here:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49561

It's more of a case of compatibility with older versions of gcc. Looks like they really don't want to add an additional "data member".

Quoting:

This patch made c++98 and c++11 code incompatible and is causing serious problems for distros.

Where the patch is the fix they implemented for gcc 4.7 (it was O(1) in it).

Another quote:

maintaining ABI compatibility has been decided to be more important for the current releases


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...