title: C++ preprocessor test if class member exists
body: <p>Is there an equivalent of #ifdef to test if a member exists in a class so that processing can be done without causing the code to fail the compiler. I have tried template operations but the particular problem has not succeeded.</p>
  
  <p>For example</p>
  
  <pre><code>#if member baseclass.memberA()
    baseclass.memberA().push_back(data);
  #else
    doAlternate(data);
  #endif
  </code></pre>
  
  <p>Obviously the above is not valid, but I am trying to discover if something like this has been added to C++11</p>
  
  <p>Note that in the initial setup, there will exist memberA, memberB, memberC, ... each of which will require the push_back. Other members will be added to the baseclass in the future, which is why I want to create a template so that all the cases will compile and process properly even if the current baseclass does not have some of the members (such as memberX). Otherwise, I can just put in the push_back() line with a very simple template.</p>
  
  <p>This is actually the simplest case. There is also the case in which I create an instantiation of the subclass and then push it back into the subclass member.</p>
  
  <pre><code>// Instantiate an element of the Maindata class
  ::basedata::Maindata maindata;
  //Instantiate an element of the Subdata class
  ::basedata::Subdata subinfo("This goes into the subinfo vector");
  // Process some data that is part of the Subdata class
  subinfo.contexts(contextInfo);
  // Push the instantiated Subdata into the Subdata member of Maindata
  maindata.subdata().push_back(subinfo);
  </code></pre>
  
  <p>Note that both Subdata and subdata() need to be set up so that the appropriate code is implemented. However, if ::basedata::Subdata exists then so will maindata.subdata().</p>
  
  <p>I have already tried various methods using templates and the particular problem has not been solvable with the various answers received. Examples are <a href="https://stackoverflow.com/questions/28653815/template-instantiation-check-for-member-existing-in-class">template instantiation check for member existing in class</a>, <a href="https://stackoverflow.com/questions/28657982/c-class-member-check-if-not-a-template">C++ class member check if not a template</a>, <a href="https://stackoverflow.com/questions/28680879/c-template-for-variable-type-declaration">C++ template for variable type declaration</a></p>