std::distance() for pointers to data members

A

Alex Vinokur

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

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".
 
N

Noah Roberts

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top