C ++类成员检查如果不是模板 [英] C++ class member check if not a template

查看:110
本文介绍了C ++类成员检查如果不是模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于存在于类中的成员的模板实例化检查说明如何检查模板中是否存在类成员。然而,给定交换机内的一组进程( NOT 模板)是有办法处理成员检查的情况。它应该类似这样的东西。注意,实际的类定义不在我的控制之下,并且正在使用的头文件和库的未来版本中创建。

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;
    }


推荐答案

您想要的工作:

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

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

然后在 case

第一个重载将被实例化为具有required成员的类,而第二个重载将被实例化

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

我不认为一个非模板的方式将工作,但再次,因为这些模板可以放在你的cpp文件我不看有什么缺点是他们是模板。

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.

这篇关于C ++类成员检查如果不是模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆