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.
For example
#if member baseclass.memberA()
baseclass.memberA().push_back(data);
#else
doAlternate(data);
#endif
Obviously the above is not valid, but I am trying to discover if something like this has been added to C++11
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.
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.
// 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);
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().
I have already tried various methods using templates and the particular problem has not been solvable with the various answers received. Examples are template instantiation check for member existing in class, C++ class member check if not a template, C++ template for variable type declaration
Aucun commentaire:
Enregistrer un commentaire