I have a group of classes that have one or more members of the type memberA, memberB, memberC. Not all classes have all the members. I want to create a template that will set the members such as
template <typename T>
void setAttributes(t & myClass, typeA memA, typeB memB, typeC memC)
{
myClass.memberA = memA;
myClass.memberB = memb;
myClass.memberC = memC;
}
Obviously this will fail at compile time when attempting to instantiate a class that is missing one of the members. Is there a #if or something similar that will allow the check to be done for a conditional compile of the form
#ifdef myClass.memberA
myClass.memberA = memA;
#endif
I do not have access right now to actually try this or anything similar and I would like to know if there is a valid way of setting it up.
I have seen references to SFINAE ("substitution failure is not an error.") but I am not sure how it would be used in this case. Is the suggestion below correct?
Substitution failure is not an error example would seem to imply that I should create a separate function for each member with a duplicate function without that member.
template <typename T>
void setMemberA(T & myClass, typeA memA)
{
myClass.memberA = memA;
}
template <typenum T>
void setMemberA(T & myClass)
{
// This is a dummy template to avoid a compilation problem
}