1

template instantiation check for member existing in class explains how to check if a class member exists in a template. However, given a set of processes within a switch (NOT a template) is there a way to handle a member check case. It should be similar to something like this. Note that the actual class definition is not under my control and is being created in a future release of header files and libraries that I am using.

I am aware that this preprocessor example would not work, but since this is not a template, how would this processing be set up?

    case myCase:
    {
#ifdef myClass.memberA
      myClass.memberA varName;
      // other processing using varName
#else
      //Alternate processing
#endif
      break;
    }
Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • The preprocessor isn't aware of any c++ constructs. The canonical way for doing such things is using SFINAE. In other words: It cannot be done with non template code. – πάντα ῥεῖ Feb 22 '15 at 12:50
  • If you look around a little there are plenty of example of using [SFINAE](http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error) to check if members exists, for example [this old question](http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence). – Some programmer dude Feb 22 '15 at 12:53
  • BTW why the requirement for non-template code? – Motti Feb 22 '15 at 15:32
  • @Motti This is a part of a large set of processing using a switch and case set of operations with each case using different processes. If they used the same processes for different members, then I could create a template. The various members for each case will be added to a future release of software that I am including. I want to write the future code so that it will automatically be picked up as the new libraries and header files get added. If I use something like #ifdef FUTURE, I will have to create the new definitions by hand and redo the code. – sabbahillel Feb 22 '15 at 19:21

1 Answers1

2

You can have two template overloads of the work you want:

template<class T>
void process_myCase(T& obj, std::true_type);

template<class T>
void process_myCase(T& obj, std::false_type);

Then in your case call the function with the second parameter computed by the method you mention in the beginning of the question.

The first overload will be instantiated for classes with the required member while the second overload will be instantiated for all the rest.

I don't think a non-templated way will work but then again since these templates can be placed in your cpp file I don't see what drawback there is for them being templates.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • 1
    I see in the comment that one can set up a template to make the determination. However, one would the have to build a template using the result to perfom the action. However, if I build the overloads as functions and not as templates, wouldn't I get the compiler error anyways? The question is, how do I put the declaration in the case statement without building a template? – sabbahillel Feb 22 '15 at 14:58
  • On second thought you may be right since these methods are not templates they must both be compiled (even if one is later discarded). Can you give it a try and let me know what happens? – Motti Feb 22 '15 at 15:10
  • @sabbahillel I've updated my answer to make the overloads templates. I'm sorry that this doesn't meet the constraints of your question but it's the best I can think of :( – Motti Feb 22 '15 at 21:04
  • Thanks. The problem is that I have a large number of cases and I would have to create a pair of templates for every case which could get unwieldy. Hopefully, it will get updated soon enough so that the actual cases can be built for use. – sabbahillel Feb 22 '15 at 21:45
  • Thanks after checking the code, (I had to wait to get in to where I have access) I now see how this applies to my situation. – sabbahillel Feb 23 '15 at 16:26