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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:28:53+00:00 2026-06-05T21:28:53+00:00

Recently i made two template classes,according to the book Modern C++ design. I think

  • 0

Recently i made two template classes,according to the book “Modern C++ design”. I think these classes are useful but no one in my company agree with me,so could any one tell me if these things are useful?

The first one is a parameter wrapper,it can package function paramters to a single dynamic object.It looks like TypeList in “Modern C++ design”.

You can use it like this:

some place of your code:

int i = 7;
bool b = true;
double d = 3.3;
CParam *p1 = CreateParam(b,i);
CParam *p2 = CreateParam(i,b,d);

other place of your code:

int i = 0;
bool b = false;
double d = 0.0;
GetParam(p1,b,i);
GetParam(p2,i,b,d);

The second one is a generic callback wrapper,it has some special point compare to other wrappers:
1.This template class has a dynamic base class,which let you use a single type object represent all wrapper objects.
2.It can wrap the callback together with it’s parameters,you can excute the callback sometimes later with the parameters.

You can use it like this:

somewhere of your code:

void Test1(int i)
{
}

void Test2(bool b,int i)
{
}

CallbackFunc * p1 = CreateCallback(Test1,3);
CallbackFunc * p2 = CreateCallback(Test2,false,99);

otherwhere of your code:

p1->Excute();
p2->Excute();  

Here is a part of the codes:

parameter wrapper:

class NullType;    
struct CParam
{
    virtual ~CParam(){}
};

template<class T1,class T2>
struct CParam2 : public CParam
{
    CParam2(T1 &t1,T2 &t2):v1(t1),v2(t2){}
    CParam2(){}
    T1 v1;
    T2 v2;
};

template<class T1>
struct CParam2<T1,NullType> : public CParam
{
    CParam2(T1 &t1):v1(t1){}
    CParam2(){}
    T1 v1;
};

template<class T1>
CParam * CreateParam(T1 t1)
{
    return (new CParam2<T1,NullType>(t1));
}

template<class T1,class T2>
CParam * CreateParam(T1 t1,T2 t2)
{
    return (new CParam2<T1,T2>(t1,t2));
}
template<class T1,class T2,class T3>
CParam * CreateParam(T1 t1,T2 t2,T3 t3)
{
    CParam2<T2,T3> t(t2,t3);
    return new CParam2<T1,CParam2<T2,T3> >(t1,t);
}

template<class T1>
void GetParam(CParam *p,T1 &t1)
{
    PARAM1(T1)* p2 = dynamic_cast<CParam2<T1,NullType>*>(p);
    t1 = p2->v1;
}

callback wrapper:

#define PARAM1(T1) CParam2<T1,NullType>
#define PARAM2(T1,T2) CParam2<T1,T2>
#define PARAM3(T1,T2,T3) CParam2<T1,CParam2<T2,T3> >    

class CallbackFunc
{
public:
    virtual ~CallbackFunc(){}
    virtual void Excute(void){}
};

template<class T>
class CallbackFunc2 : public CallbackFunc
{
public:
    CallbackFunc2():m_b(false){}
    CallbackFunc2(T &t):m_t(t),m_b(true){}
    T m_t;
    bool m_b;
};


template<class M,class T>
class StaticCallbackFunc : public CallbackFunc2<T>
{
public:
    StaticCallbackFunc(M m):m_m(m){}

    StaticCallbackFunc(M m,T t):CallbackFunc2<T>(t),m_m(m){}

    virtual void Excute(void){assert(CallbackFunc2<T>::m_b);CallMethod(CallbackFunc2<T>::m_t);}

private:
    template<class T1>
    void CallMethod(PARAM1(T1) &t){m_m(t.v1);}

    template<class T1,class T2>
    void CallMethod(PARAM2(T1,T2) &t){m_m(t.v1,t.v2);}

    template<class T1,class T2,class T3>
    void CallMethod(PARAM3(T1,T2,T3) &t){m_m(t.v1,t.v2.v1,t.v2.v2);}

private:
    M m_m;
};


template<class M>
class StaticCallbackFunc<M,void> : public CallbackFunc
{
public:
    StaticCallbackFunc(M method):m_m(method){}
    virtual void Excute(void){m_m();}
private:
    M m_m;
};

template<class C,class M,class T>
class MemberCallbackFunc : public CallbackFunc2<T>
{
public:
    MemberCallbackFunc(C *pC,M m):m_pC(pC),m_m(m){}

    MemberCallbackFunc(C *pC,M m,T t):CallbackFunc2<T>(t),m_pC(pC),m_m(m){}

    virtual void Excute(void){assert(CallbackFunc2<T>::m_b);CallMethod(CallbackFunc2<T>::m_t);}

    template<class T1>
    void CallMethod(PARAM1(T1) &t){(m_pC->*m_m)(t.v1);}

    template<class T1,class T2>
    void CallMethod(PARAM2(T1,T2) &t){(m_pC->*m_m)(t.v1,t.v2);}

    template<class T1,class T2,class T3>
    void CallMethod(PARAM3(T1,T2,T3) &t){(m_pC->*m_m)(t.v1,t.v2.v1,t.v2.v2);}

private:
    C *m_pC;
    M m_m;
};


template<class T1>
CallbackFunc *CreateCallback(CallbackFunc *p,T1 t1)
{
    CParam2<T1,NullType> t(t1);
    return new StaticCallbackFunc<CallbackFunc *,CParam2<T1,NullType> >(p,t);
}

template<class C,class T1>
CallbackFunc *CreateCallback(C *pC,void(C::*pF)(T1),T1 t1)
{
    CParam2<T1,NullType>t(t1);
    return new MemberCallbackFunc<C,void(C::*)(T1),CParam2<T1,NullType> >(pC,pF,t);
}

template<class T1>
CParam2<T1,NullType> CreateCallbackParam(T1 t1)
{
    return  CParam2<T1,NullType>(t1);
}

template<class T1>
void ExcuteCallback(CallbackFunc *p,T1 t1)
{
    CallbackFunc2<CParam2<T1,NullType> > *p2 = dynamic_cast<CallbackFunc2<CParam2<T1,NullType> > *>(p);
    p2->m_t.v1 = t1;
    p2->m_b = true;
    p->Excute();
}
  • 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-06-05T21:28:54+00:00Added an answer on June 5, 2026 at 9:28 pm

    They look useful to me. In the standard library, there is std::tuple and std::function which do similar things.

    So, if it’s worth having something like this in the standard library, lots of other people probably think being able to write code using patterns enabled by your wrappers is useful.

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

Sidebar

Related Questions

I have recently made a program that is supposed to take two environment variables,
I recently purchased the book Filthy Rich Clients and i found it really useful
I recently made a new WordPress theme for a local travel company and they
I recently made index on a highly used view but it resulted in increase
Recently I made a git repo out of directory that already contained two git
I recently made a menu screen to my cocos2d app where I have attached
I recently made an App for IOS with Phonegap . I used the html5
I recently made some reference additions to a project in my solution and made
I recently made a very simple practice program in Python, that takes user input
I recently made this simple navigation where you have a couple links that have

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.