Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:08:51+00:00 2026-05-24T13:08:51+00:00

How can I use templates, to find out, from which types is type composed

  • 0

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

Let’s have

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

I am using MSVC2010

Thank you!

EDIT:
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:08:53+00:00Added an answer on May 24, 2026 at 1:08 pm

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

    Test code:

    int main()
    {
         typedef A<B<C<Blank> > > ComposedType;
         ComposedType ct;
         printTypeComponents(ct);
    
         cout<<"-------"<<endl;
    
         typedef A<C<Blank> > ComposedType2;
         ComposedType2 ct2;
         printTypeComponents(ct2);
    }
    

    Output:

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

    Online Demo : http://ideone.com/T5nD4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from a C++ background where I can use template mixins to write
As far as I know I can use C++ templates in CUDA device code.
I would like to learn how I can use nested template tags where the
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
In Django's template language, you can use {% url [viewname] [args] %} to generate
In VB.NET is there a library of template dialogs I can use? It's easy
how can I use the same UI template (*.ui.xml file) with multiple Java objects
I have a class factory where I'm using variadic templates for the c'tor parameters
I am struggling to find out why I can't get transform to work with
Is there any good tree manipulation (template) libraries for C++ out there that can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.