title: Getting the type of a member
body: <p><strong>NOTE</strong>: This question was originally asked way back in 2012. Before the <code>decltype</code> specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support <code>decltype</code>.</p>
  
  <p>Is there an easy way to retrieve the type of a member?<br>
  In C++03</p>
  
  <pre><code>struct Person
  {
      std::string name;
      int         age;
      double      salary;
  };
  
  int main()
  {
      std::vector&lt;Person&gt;     people; //  get a vector of people.
  
      std::vector&lt;GET_TYPE_OF(Person::age)&gt;   ages;
  
      ages.push_back(people[0].age);
      ages.push_back(people[10].age);
      ages.push_back(people[13].age);
  
  }
  </code></pre>
  
  <p>I am actually doing this (ie being slightly lazy):</p>
  
  <pre><code>#define BuildType(className, member, type)                                 \
          struct className ## member: TypeBase&lt;className, type&gt;              \
          {                                                                  \
              className ## member()                                          \
                  : TypeBase&lt;className, type&gt;(#member, &amp;className::member)   \
              {}                                                             \
          }
  
  BuildType(Person, name,     std::string);
  BuildType(Person, age,      int);
  BuildType(Person, salary,   double);
  typedef boost::mpl::vector&lt;Personname, Personage, Personsalary&gt; FunckyMTPMap;
  </code></pre>
  
  <p>But rather than have to force the user to specify the type of the member I want to the compiler to generate it pragmatically.</p>
  
  <pre><code>#define BuildType(className, member)                                                  \
  struct className ## member: TypeBase&lt;className, TYPE_OF(className ## member)&gt;         \
  {                                                                                     \
     className ## member()                                                              \
        : TypeBase&lt;className, TYPE_OF(className ## member)&gt;(#member, &amp;className::member)\
     {}                                                                                 \
  }
  BuildType(Person, name);
  BuildType(Person, age);
  BuildType(Person, salary);
  typedef boost::mpl::vector&lt;Personname, Personage, Personsalary&gt; FunckyMTPMap;
  </code></pre>