Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::distance() for pointers to data members (http://www.velocityreviews.com/forums/t745613-std-distance-for-pointers-to-data-members.html)

Alex Vinokur 03-24-2011 06:46 AM

std::distance() for pointers to data members
 
Hi,

Is it possible to use std::distance() for pointers to data members?

Thanks

------ foo. cpp ------
#include <iterator>

struct Foo
{
char m_ch1;
char m_ch2;
};

int main()
{
char (Foo::* p1) = &Foo::m_ch1;
char (Foo::* p2) = &Foo::m_ch2;

std::distance(p1, p2);

return 0;
}
---------------

Compiler aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]


"/opt/aCC/include_std/rw/iterator", line 99: error #2276: name
followed by "::" must be a class or namespace name
typedef _TYPENAME _Iterator::value_type value_type;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with _Iterator=char Foo::*]" at line
14 of "foo.cpp"

"/opt/aCC/include_std/rw/iterator", line 100: error #2276: name
followed by "::" must be a class or namespace name
typedef _TYPENAME _Iterator::difference_type difference_type;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with _Iterator=char Foo::*]" at line
14 of "foo.cpp"

"/opt/aCC/include_std/rw/iterator", line 101: error #2276: name
followed by "::" must be a class or namespace name
typedef _TYPENAME _Iterator::pointer pointer;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with _Iterator=char Foo::*]" at line
14 of "foo.cpp"

"/opt/aCC/include_std/rw/iterator", line 102: error #2276: name
followed by "::" must be a class or namespace name
typedef _TYPENAME _Iterator::reference reference;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with _Iterator=char Foo::*]" at line
14 of "foo.cpp"

"/opt/aCC/include_std/rw/iterator", line 103: error #2276: name
followed by "::" must be a class or namespace name
typedef _TYPENAME _Iterator::iterator_category
iterator_category;
^
detected during instantiation of class
"std::iterator_traits<_Iterator> [with _Iterator=char Foo::*]" at line
14 of "foo.cpp"

5 errors detected in the compilation of "foo.cpp".


Öö Tiib 03-24-2011 07:07 AM

Re: std::distance() for pointers to data members
 
On 24 märts, 08:46, Alex Vinokur <alex.vino...@gmail.com> wrote:
> Hi,
>
> Is it possible to use std::distance() for pointers to data members?


No.
http://www.cplusplus.com/reference/s...ator/distance/

It is working with iterators in same container, so pointers to same
array qualify, not pointers to different data members.

Öö Tiib 03-24-2011 07:51 AM

Re: std::distance() for pointers to data members
 
On Mar 24, 9:07*am, Öö Tiib <oot...@hot.ee> wrote:
> It is working with iterators in same container, so pointers to same
> array qualify, not pointers to different data members.


I meant "pointers to elements of same array".

Noah Roberts 03-24-2011 04:33 PM

Re: std::distance() for pointers to data members
 
On 3/23/2011 11:46 PM, Alex Vinokur wrote:
> Hi,
>
> Is it possible to use std::distance() for pointers to data members?


No. Even if you somehow managed to make it compile you wouldn't want
to. std::distance uses iterator/pointer arithmetic on its parameters.
In order for the behavior of that use to be defined, you must be able to
"reach" the second parameter from the first. Only iterators to the same
containers and pointers to elements in the same array are reachable
to/from each other.

I can't think of a situation in which doing pointer arithmetic on a
member pointer results in defined behavior. Consider that
(array+array_size) is only defined because of a special clause in the
standard that says the one-past-end pointer is valid. On the other
hand, (array+array_size+1) is undefined. Contrary to popular opinion,
an implementation is free to freak out if you try to increment the
past-end pointer or any other pointer that can't validly "reach" the
next address.

I don't know of any way to cause a pointer to member to point at an
element within an array. It can be an element, but can't point at one.
Only if you could would it be possible to use std::distance on member
pointers.

--
http://crazycpp.wordpress.com


All times are GMT. The time now is 11:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.