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 6251433
In Process

The Archive Base Latest Questions

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

ok, yesterday I posted almost identical question here , but I wasn’t able to

  • 0

ok, yesterday I posted almost identical question here
, but I wasn’t able to modify the answer(working) to my needs… I did not want to mess the other topic, so I have started new one.

So, I have 2 (actually about 15) structs, which can composed an object

class MyBase{};

template <typename Super, typename T1, typename T2> 
struct A : public Super 
{
    void doStuffA() { cout<<"doing something in A"; }
};

template <typename Super, typename T1, typename T2> 
struct B : public Super 
{
    void doStuffB() { cout<<"doing something in B"; }
};

then I have:

template <typename ComposedType, typename T1, typename T2>
class Combined
{
    ComposedType m_cT;
public:
    Combined(const ComposedType & c) : m_cT(c) { }

    typedef A<null, T1, T2> anull;
    typedef B<null, T1, T2> bnull;

    void update()
    {
        typedef typename split<ComposedType>::Ct Ct;
        typedef typename split<ComposedType>::At At;

        //this I want 
        if( composed of A ) 
            m_cT.doStuffA();

        if( composed of B ) 
            m_cT.doStuffB();
    }
};

and I want to use it like:

int main()
{
    typedef A<B<MyBase,int,int>,int,int> ComposedType1;
    typedef B<MyBase,int,int> ComposedType2;

    ComposedType1 ct1;
    ComposedType2 ct2;

    Combined<ComposedType1, int, int> cb1(ct1);
    cb1.update();

    Combined<ComposedType2, int, int> cb2(ct2);
    cb2.update();
}

(ints are just for example purposes)

So I have some template magic:

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<MyBase> >
{
    typedef C<null> Ct; //class template 
    typedef MyBase   At;  //argument type
};

but I can not make it works 🙁

I know there is a lot of code, but this is actually minimal example… I have posted this code to ideone, to make it better for reading.

Thank you!

EDIT: (to ask questions in comments)

I am building system for AI and want to solve as much thing in compile
time as I can. In this case, I am building system for movement behavior.
My code supply many types of behavior like “Go to point”, “Evade from”,
“Avoid obstacles” etc. This behaviors are in example above
mentioned as A a, B. Each of this behavior has method like “performBehavior”
and its return type can be combined with other “performBehavior”.

So I want to put together specific behavior at compile time. eg. just A or A+C+D+F etc…

and then in my update do something like:

if behavior is consisted of “Go to point”, than “performBehaviorGoTo”

if behavior is consisted of “Evade from”, than “performBehaviorEvade”

…

this is very very short explanation, but hope I have made my point

  • 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:36:51+00:00Added an answer on May 24, 2026 at 1:36 pm

    You can do that with function overloading:

    template <typename Super, typename T1, typename T2> 
    void doStuff(A<Super, T1, T2>& a) { a.doStaffA(); }
    
    template <typename Super, typename T1, typename T2> 
    void doStuff(B<Super, T1, T2>& b) { b.doStaffB(); }
    

    And then:

    // ...
    void update()
    {
        //this I want 
        //if( composed of A ) 
        //    m_cT.doStuffA();
        //if( composed of B ) 
        //    m_cT.doStuffB();
    
        doStuff(m_cT);
    }
    

    It is not clear, whether you want to chain calls for A<B<...> >. If you do, then something like the following would do:

    template <class T> 
    void doStuff(T&) { /* do nothing */ }
    
    template <typename Super, typename T1, typename T2> 
    void doStuff(A<Super, T1, T2>& a) { 
        a.doStaffA(); 
        doStuff(static_cast<Super&>(a)); 
    }
    
    template <typename Super, typename T1, typename T2> 
    void doStuff(B<Super, T1, T2>& b) { 
        b.doStaffB(); 
        doStuff(static_cast<Super&>(b)); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've posted a question yesterday but I just realized that the answer doesn't seem
I posted the following question yesterday but didn't get a helpful answer. May i
Yesterday I posted a question here ( FxCop and Code Analysis Rules ) about
Yesterday, I posted an answer to a question that included several (unknown to me
Following on from a question I posted yesterday. Found here: Append different div id
I posted this question yesterday and the answer I go mentioned that I should
Here is my JSFiddle . Yesterday I posted question that how to create hashtag
I posted a question yesterday here: Finding and adding to a .kml file using
this is a rephrasing of a question I posted yesterday. I got an answer
this is a follow-up to the question I posted yesterday. I was able to

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.