Typedef declaration syntax

A few days ago I looked at the boost sources and found an interesting typedef .

There is 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 have not seen such syntax before and cannot explain the meaning of it.

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

What is syntax?

What's the difference between " typedef int none_helper :: * none_t " and, for example, " typedef int * none_t "?

+2


a source to share


3 answers


The syntax is for a pointer to a member - here it's typedefs none_t

as a pointer to a data member int

none_helper

.

The syntax can be used for example. in the following 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 for more details.

+3


a source


  • typedef int* none_t;

    introduces a type alias for a pointer to an integer.
  • typedef int non_helper::*none_t;

    introduces a type alias for a pointer to an integer member of the class non_helper

    .


+1


a source


none_t

is a pointer to a member variable of type int

none_helper

.

struct none_helper
{
  int x1;
  int x2;
};

int none_helper::* ptm = &none_helper::x1;
^^^^^^^^^^^^^^^^^^
     none_t

      

+1


a source







All Articles