views:

138

answers:

3

Some days ago I looked at boost sources and found interesting typedef.

There is a code from "boost\detail\none_t.hpp":

namespace boost {

namespace detail {

struct none_helper{};

typedef int none_helper::*none_t ;

} // namespace detail

} // namespace boost

I didn't see syntax like that earlier and can't explain the sense of that.

This typedef introduces name "none_t" as pointer to int in boost::detail namespace.

What the syntax is?

And what difference between "typedef int none_helper::*none_t" and for example "typedef int *none_t" ?

+2  A: 

The syntax is for a pointer to member - here it typedefs none_t as a pointer to an int data member of none_helper.

The syntax can be used e.g. this way:

 struct X { int i; };

 typedef int X::*PI;
 PI pi = &X::i;
 X* x = foo();
 x->*pi = 42;

InformIT has an article on member pointers, containing more details.

Georg Fritzsche
+1  A: 
  • typedef int* none_t; introduces type alias for pointer to integer.
  • typedef int non_helper::*none_t; introduces type alias for pointer to integer member of non_helper class.
Nikolai N Fetissov
+1  A: 

none_t is a pointer to member variable with type int of none_helper.

struct none_helper
{
  int x1;
  int x2;
};

int none_helper::* ptm = &none_helper::x1;
^^^^^^^^^^^^^^^^^^
     none_t
Kirill V. Lyadvinsky