Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 827
Martin Drozdik Avatar asked Oct 03 '13 08:10

Martin Drozdik


1 Answers

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

like image 75
tomi.lee.jones Avatar answered Oct 16 '22 05:10

tomi.lee.jones