--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
There is no performance advantage either in providing 'data()' from doing 'vector.empty()? 0 : &vector.front()', or 'vector.empty()? 0 : &*vector.begin()'.
The question, I understood, is more of a style than an implementation. Of course in this case the difference in style is lesser (since the conditional is already handled inside 'data()' and need not be replicated, so it might not be worth it, but it should not only be considered in terms of performance.
Just to clarify my main issue was more obvious user code rather than efficiency. I'm sure a good compiler would optimize vec.data( ) + vec.end( ) to be the same as vec.end_data( ). I guess it depends on the implementation of vector, as mentioned by Tiago earlier, which as a user of vector I don't care about. I suspect a vec::end_data would never end up being less efficient than vec.data( ) + vec.end( ) or friends.
If we don't care about simpler user code why add vec.data()? &vec.front() or equivalents work just as well. Given the following
c_func( &vec.front( ), &vec.end( ) + 1 );
c_func( vec.data( ), vec.data() + vec.size( ) );
Fine, but that is still missing the point.
There are proposed changes and changes that went into the standard that are just stylistic.
Consider a different case:
vector<T>::cbegin() vs. casting to 'const vector<T>&' and calling 'begin()'
No performance there. Same comment as in previous message: this is a different level of improvement in style, 'data()+size()' is not as ugly as the cast above.
Em seg 07 abr 2014, às 11:29:59, Greg Marr escreveu:
> On Monday, April 7, 2014 12:04:28 PM UTC-4, David Rodríguez Ibeas wrote:
> > There is no performance advantage either in providing 'data()' from doing
> > 'vector.empty()? 0 : &vector.front()', or 'vector.empty()? 0 :
> > &*vector.begin()'.
>
> Yes there is. It eliminates a check to see if the vector is empty.
The check is not necessary because:
> There is no conditional in std::vector::data(). It simply returns the
> pointer to the internal array storage.
end = begin + size
size = 0
∴ end == begin
and when you have a range of [begin, end) with begin == end, the range is
empty, no matter what the value of begin is. You don't need to perform any
checks.
Consider a different case:
vector<T>::cbegin() vs. casting to 'const vector<T>&' and calling 'begin()'No performance there. Same comment as in previous message: this is a different level of improvement in style, 'data()+size()' is not as ugly as the cast above.That is an extremely different level. There is really no comparison between adding v.cbegin() to replace (static_cast<vector<std::vector<std::wstring>> &>(v)).begin(); and adding v.end_data() to replace v.data() + v.size()
Sorry, I think I had misunderstood you.
Yes, data() is a performance advantage, since you can't call front() or
*begin() on an empty container.
My point is that end_data() is not a performance advantage. It's just
syntactic sugar.
Actually the comment about performance with a empty vector made me look at the standard.
So my question is what does this mean for an empty vector.
I am wondering if there is any guarantee that the data function never returns nullptr.
My understanding of the standard is that adding 0 to a nullptr is valid and you get a nullptr.
So [data(),data() + size()) would be two nullptrs.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
in case of using pointers as iterators, validity if iterators means none-null pointers,
since validity of a pointer **does mean** the validity of what it points to,
By this observation, we can see that if 'size()' equals to 0,then [data(),data() + size()) is an invalid range regardless of validity of 'data().