如何检查类是否在C ++ 11中指定了嵌套类定义或typedef? [英] How to check whether a class has specified nested class defination or typedef in C++ 11?

查看:84
本文介绍了如何检查类是否在C ++ 11中指定了嵌套类定义或typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想实现一些现有更大的类的模板代理类。现有的类是库类,因此无法修改。在大多数情况下,客户端不知道对象是代理类或更大类的实例。然而,在某些情况下,客户端必须知道详细的类信息。因为代理类本身是一个模板类,我不认为简单的函数重载类名可以解决这个问题。我想到的可能的解决方案是在代理类中添加一个内部嵌套类或typedef,并且客户端检查这个类/ typedef是否存在来获取类信息。我的问题是:如何检查类是否在C ++ 11中指定了嵌套类定义或typedef?

In my project, I want to implement a template proxy class of some existing bigger classes. The existing classes are library classes, so they can not be modified. In most cases, the clients do not know the objects are instances of proxy class or bigger class. In some cases, however, the clients MUST know the detailed class information. Since the proxy class is itself a template class, I do not think simple function overloading by class name could solve this problem. The possible solution I thought is to add an internal nested class or typedef inside the proxy class, and the client check whether this class/typedef exists to get the class information. My question is: how to check whether a class has specified nested class defination or typedef in C++ 11 ?

以下代码显示了一个示例:

The following codes show an example:

#include <iostream>
#include <functional>
#include <string>
#include <vector>
#include <type_traits>

typedef std::string CBig1;  //  use string for demonstration
typedef std::string CBig2;  //  use string for demonstration

//class CBig1;   // the bigger class 1, codes of which can not be changed
//class CBig2;   // the bigger class 2, codes of which can not be changed

template <typename _Big, typename _Other>
class CProxy
{
public:
    struct proxy_tag { };
};

//  how to implement this ?
//  the proxy traits class, if defined _T::proxy_tag, the ``type'' will be std::true_type, otherwise the ``type'' will be std::false_type
template <typename _T>
struct is_proxy
{
    //typedef std::true_type type;
    //typedef std::false_type type;
};

template <typename _T>
void ClientHelp(const _T& t, std::false_type)
{
    //  process real class
    std::cerr << "real class" << std::endl;
}

template <typename _T>
void ClientHelp(const _T& t, std::true_type)
{
    //  process proxy class
    std::cerr << "proxy class" << std::endl;
}

template <typename _T>
void Client(const _T& t)
{
    ClientHelp(t, typename is_proxy<_T>::type());
}

int main(int argc, char* argv[])
{
    CBig1 b;
    CProxy<CBig1, int> p;
    Client(b);
    Client(p);
    return 0;
}

如何实现traits类 is_proxy ?提前感谢。

How to implement the traits class is_proxy ? Thanks in advance.

推荐答案

作为C ++ 03版本的补充,在C ++ 11中, $ c> decltype :

As a complement to the C++03 version, in C++11 you get decltype:

template <typename T>
auto is_proxy(T const&) -> decltype(T::proxy_tag{}, std::true_type{}) {
  return std::true_type{};
}

std::false_type is_proxy(...) { return std::false_type{}; }

您的 Client

template <typename T>
void Client(T const& t) {
  ClientHelp(t, is_proxy(t));
}

Sweet,不是吗?

Sweet, isn't it ?

这篇关于如何检查类是否在C ++ 11中指定了嵌套类定义或typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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