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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:04:15+00:00 2026-05-15T13:04:15+00:00

Please consider this -probably poorly written- example : class Command; class Command : public

  • 0

Please consider this -probably poorly written- example :

class Command;

 class Command : public  boost::enable_shared_from_this<Command>
 { 
  public :
   void execute()
   { 
    executeImpl();
                // then do some stuff which is common to all commands ... 
   }

   // Much more stuff ...
     private:
      virtual void executeImpl()=0;
         // Much more stuff too ...
 };

and :

class CmdAdd : public Command
 {
 public:
  CmdAdd(int howMuchToAdd);
  void executeImpl();


  int _amountToAdd;
 };

// implementation isn't really important here .... 

With this, I can simply add a callback using this syntax :

        boost::shared_ptr<Command> cmdAdd(CmdAdd(someValue));
     cmdAdd->execute();

It works flawlessly. My “Command” class does much more things which are common to all commands, such as implementing undo, redo, progress report and so on, but I removed it from the code for the sake of readability.

Now my question is simple :
is there a way to rewrite the command class, so that I can replace this call :

boost::shared_ptr<Command> cmdAdd(CmdAdd(someValue));
cmdAdd->execute();

by something like :

CmdAdd(someValue); // preferably
or CmdAdd->execute(someValue)

I’ve been thinking about that a lot but I have a conceptual problem :
I wanted to template my Command class like

template <typename R,typename T1, typename T2, ..., typename Tn> class Command
{
    R1 execute(T1 p1, ...,Tn pn)
    { 
        return executeImpl(T1 p1, ...,Tn pn);
        // then do some stuff which is common to all commands ... 
    }
}

but obviously, there’s a problem here :
the syntax template <typename R,typename T1, typename T2, ..., typename Tn> isn’t legal C++ , AFAIK.

Do I have to write n versions of Command, like :

template <typename R> class Command
template <typename R,typename T1> class Command
template <typename R,typename T1, typename T2> class Command
...

and so on ?
(not even sure this is gonna work indeed)

Or is there another, more elegant way to do this ?
Is the syntax, mentioned here of any use there ? (function f;)

I’ve been looking at Loki’s type lists and they seem to do the job. But I can’t find anything that in Boost. I read on the web that boost::mpl is what one wants to use to implement typelists, but I’m a bit confused by MPL docs ?

Any insights on this ?
Regads,
D.

  • 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-15T13:04:16+00:00Added an answer on May 15, 2026 at 1:04 pm

    Interesting question 🙂

    First of all, there is an issue you overlooked: you need a common base class for all Command and this class cannot be templated if you are going to use a stack of them (for undo/redo).

    Therefore you are stuck with:

    class Command
    {
    public:
      void execute(); 
    private:
      virtual void executeImpl() = 0;
    };
    

    I can understand your desire an execute function with parameters, but don’t forget that anyway you would need to save those parameters for the undo/redo operation. It’s just simpler to get them through the constructor.

    However, you could still use a templated method to actually invoke a command:

    template <class Command>
    void execute() { Command cmd; cmd.execute(); }
    
    template <class Command, class T0>
    void execute(T0& arg0) { Command cmd(arg0); cmd.execute(); }
    
    /// ...
    
    int main(int argc, char* argv[])
    {
      execute<MyLittleCommand>("path", 3);
    }
    

    Which is close to the syntax you desired. Note that I have purposely forgotten about the stack here, in my mind you need to pass it to the execute method for registration (once completed).

    Not that I would also probably change the Command design to get closer to a Strategy pattern:

    struct CommandImpl
    {
      virtual ~CommandImpl();
      virtual void executeImpl() = 0;
    };
    
    class Command
    {
    public:
      template <class C>
      static Command Make() { return Command(new C()); }
    
      template <class C, class T0>
      static Command Make(T0& arg0) { return Command(new C(arg0)); }
    
      /// ....
    
      void execute(CommandStack& stack)
      {
        mImpl->executeImpl();
        stack.Push(*this);
      }
    
    private:
      Command(CommandImpl* c): mImpl(c) {}
      boost::shared_ptr<CommandImpl> mImpl;
    };
    

    It’s the typical combination of Non Virtual Interface and Pointer to Implementation idioms.

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

Sidebar

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would change var test = 'true'; to test =… May 16, 2026 at 4:59 am
  • Editorial Team
    Editorial Team added an answer Now the compiler does not give any warning. The compiler… May 16, 2026 at 4:59 am
  • Editorial Team
    Editorial Team added an answer If the delegate method is fired for the Left Split… May 16, 2026 at 4:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.