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

4
  • What are you trying to do? And why? If you want to know the parameters to A, add some typedefs there? If you want to "do_stuff" differently depending on the type, make the function virtual.
    – Bo Persson
    Aug 10, 2011 at 15:39
  • Are you experimenting? Or you want to write such complex code? What problem are you trying to solve with such complex coding? Aug 10, 2011 at 15:41
  • @Nawaz please, see my Edit to answer you questions
    – relaxxx
    Aug 10, 2011 at 20:19
  • @BoPersson please, see my Edit to answer you questions
    – relaxxx
    Aug 10, 2011 at 20:19

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.