[Range] size and list, for example.

I know this has come up before, so I'm really seeking to discover the current situation. boost::size( std::list<...> ) fails because the implementation of size is end - begin. I know I could use distance, but am surprised size(list) doesn't work out of the box. * So, is it correct that size(list) doesn't work? * Does distance(begin, end) specialise for random access iterators to O(n) performance? * Why doesn't size similarly specialise? Thx, Rob.

I know this has come up before, so I'm really seeking to discover the current situation.
boost::size( std::list<...> ) fails because the implementation of size is end - begin.
I know I could use distance, but am surprised size(list) doesn't work out of the box.
* So, is it correct that size(list) doesn't work?
* Does distance(begin, end) specialise for random access iterators to O(n) performance?
* Why doesn't size similarly specialise?
size() and distance() have different purposes. size() is for when you want to know the size of a range in constant time, and do something else if it cannot be determined in constant time. distance() is for when you want to know the size of a range regardless of whether it takes constant time or linear time to calculate. Note that Boost.Range provides a distance(range) overload of distance, so it's not any more verbose to use than size() if your use case is the second one. Regarding std::list, there are different stories for C++98 and C++11: In C++98, the complexity of std::list::size() was not guaranteed to be constant [1]. My understanding is that some implementations didn't keep track of the size in order to implement constant-time splice(), and consequently their size() was linear-time. So I think that it is not reasonable to expect that size() works for std::list in C++98. In C++11, however, the complexity of std::list::size() is guaranteed to be constant [1]. Therefore, I think it would be reasonable if in C++11, Boost.Range provided a specialized version of size() for std::list that forwarded to std::list::size(). This has not been implemented yet. If you would like to see it implemented, please file a Trac ticket. If no one objects to this addition, I will implement it when I get a chance. Regards, Nate [1] http://www.cplusplus.com/reference/list/list/size/

On Fri, Apr 12, 2013 at 7:17 AM, Nathan Ridge
In C++11, however, the complexity of std::list::size() is guaranteed to be constant [1]. Therefore, I think it would be reasonable if in C++11, Boost.Range provided a specialized version of size() for std::list that forwarded to std::list::size(). This has not been implemented yet. If you would like to see it implemented, please file a Trac ticket. If no one objects to this addition, I will implement it when I get a chance.
I definitely want to add versions of boost::size that support all of the standard containers and Boost.Containers where they the member size is O(1). I'm convinced this will be an improvement despite the remaining controversy. I believe that the inconvenience to container developers is minor relative to the frequent inconvenience to algorithm developers. If you get to implement this before me then great. The help is welcome.
Regards, Nate
Thanks, Neil Groves
participants (3)
-
Nathan Ridge
-
Neil Groves
-
Robert Jones