本文介绍了如何查找对象是由哪些模板层组成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用模板层时,如何使用模板找出类型是由哪种类型组成的?

How can I use templates, to find out, from which types is type composed of when using template layers?

让我们拥有

template <typename Super>
class A : public Super {};

template <typename Super>
class B : public Super {};

template <typename Super>
class C : public Super {};

class Blank{};

template <typename CombinedType>
void printTypeComponents(const CombinedType & t) { ... }

int main()
{
     typedef A<B<C<Blank>>> ComposedType;
     ComposedType ct;
     printTypeComponents(ct);

     typedef A<C<Blank>> ComposedType2;
     ComposedType2 ct2;
     printTypeComponents(ct2);
}

我附上我的尝试,这当然是错误的(仅当对象是由所有测试类型组成,因为测试类型实际上存在时才有效),但是您可以很容易地从中看出我的目的

I am attaching my try, wrong of course (works only if object is composed from all tested types, since tested types actually exists), but you can easily see from it, what my aim is

#include <boost/type_traits/is_base_of.hpp>
#include <iostream>

template <typename Super>
class A : public Super
{
public:
    typedef A<Super> AComponent;
};

template <typename Super>
class B : public Super
{
public:
    typedef B<Super> BComponent;
};

template <typename Super>
class C : public Super
{
public:
    typedef C<Super> CComponent;
};

class Blank{};

template <typename CombinedType>
void printTypeComponents(const CombinedType & t)
{
    if(boost::is_base_of<Blank, CombinedType::AComponent>::value)
        std::cout << "composed of A \n";

    if(boost::is_base_of<Blank, CombinedType::BComponent>::value)
        std::cout << "composed of B \n";

    if(boost::is_base_of<Blank, CombinedType::CComponent>::value)
        std::cout << "composed of C \n";
}

int main()
{
     typedef A<B<C<Blank>>> ComposedType;
     ComposedType ct;
     printTypeComponents(ct);

     //typedef A<C<Blank>> ComposedType2;
     //ComposedType2 ct2;
     //printTypeComponents(ct2);
}

我正在使用MSVC2010

I am using MSVC2010

谢谢!

我实际上对类型名称不感兴趣...我想像这样使用它:

I am not actually interested in names of types... I want to use it like:

if(composedOfA)
    doSomeCharacteristicStuffFromA(); //member function of A

if(composedOfB)
    doSomeCharacteristicStuffFromB(); //member function of B

推荐答案

我的尝试(使用C ++ 0x功能)

My attempt (without using C++0x feature(s))

//----------------------------------------

struct null{};

template<typename>
struct split
{
   typedef null Ct;
   typedef null At;
};

template<template<typename> class C, typename T>
struct split<C<T> >
{
   typedef C<null> Ct; //class template
   typedef T      At;  //argument type
};

template<template<typename> class C>
struct split<C<Blank> >
{
   typedef C<null> Ct; //class template
   typedef Blank   At;  //argument type
};

template<typename T, typename U>
struct is_same
{
   static const bool value = false;
};
template<typename T>
struct is_same<T,T>
{
   static const bool value = true;
};

typedef  A<null> anull;
typedef  B<null> bnull;
typedef  C<null> cnull;

//----------------------------------------

template <typename CombinedType>
void printTypeComponents(const CombinedType & t)
{
     typedef typename split<CombinedType>::Ct Ct;
     typedef typename split<CombinedType>::At At;

     if ( is_same<Ct,anull>::value )
           cout << "A" << endl;
     else if ( is_same<Ct,bnull>::value )
           cout << "B" << endl;
     else if ( is_same<Ct,cnull>::value )
           cout << "C" << endl;

     if ( !is_same<At,Blank>::value )
           printTypeComponents(At());
     else
           cout << "Blank" << endl;
}

测试代码:

int main()
{
     typedef A<B<C<Blank> > > ComposedType;
     ComposedType ct;
     printTypeComponents(ct);

     cout<<"-------"<<endl;

     typedef A<C<Blank> > ComposedType2;
     ComposedType2 ct2;
     printTypeComponents(ct2);
}

输出:

A
B
C
Blank
-------
A
C
Blank

在线演示: http://ideone.com/T5nD4

这篇关于如何查找对象是由哪些模板层组成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:16