C++ – unpack sub-type from template structure only for specified object type

#include <assert.h>
#include <typeinfo>
 
struct CTreeObject1
{
};
 
/************************************************************************/
/* Nammed structs - for using one type more than once in SinglesCont.   */
/************************************************************************/
template< class TObject, class TName>
struct NammedObject : public TObject, public TName 
{
        typedef TObject object_type;
};
 
struct stFirst{};
struct stSecond{};
                
template <typename T>
struct UnpackObjectType
{
        typedef T TT;
};
 
template<typename T, typename U>
struct UnpackObjectType< NammedObject<T, U> >
{
        typedef T TT; 
};
 
int main()
{
        typedef NammedObject<CTreeObject1,stFirst>  T1;
        typedef NammedObject<CTreeObject1,stSecond>  T2;
        typedef CTreeObject1                        T3;
 
        UnpackObjectType<T1>::TT d1;
        UnpackObjectType<T2>::TT d2;
        UnpackObjectType<T3>::TT d3;
        
        //required
        assert( typeid(d1) == typeid(d2));
        assert( typeid(d2) == typeid(d3));
        
        return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *