Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the best way to have a variable number of template parameters?
    primarykey
    data
    text
    <p>Please consider this -probably poorly written- example : </p> <pre><code>class Command; class Command : public boost::enable_shared_from_this&lt;Command&gt; { 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 ... }; </code></pre> <p>and : </p> <pre><code>class CmdAdd : public Command { public: CmdAdd(int howMuchToAdd); void executeImpl(); int _amountToAdd; }; // implementation isn't really important here .... </code></pre> <p>With this, I can simply add a callback using this syntax : </p> <pre><code> boost::shared_ptr&lt;Command&gt; cmdAdd(CmdAdd(someValue)); cmdAdd-&gt;execute(); </code></pre> <p>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.</p> <p>Now my question is simple : is there a way to rewrite the command class, so that I can replace this call : </p> <pre><code>boost::shared_ptr&lt;Command&gt; cmdAdd(CmdAdd(someValue)); cmdAdd-&gt;execute(); </code></pre> <p>by something like : </p> <pre><code>CmdAdd(someValue); // preferably or CmdAdd-&gt;execute(someValue) </code></pre> <p>I've been thinking about that a lot but I have a conceptual problem : I wanted to template my Command class like </p> <pre><code>template &lt;typename R,typename T1, typename T2, ..., typename Tn&gt; class Command { R1 execute(T1 p1, ...,Tn pn) { return executeImpl(T1 p1, ...,Tn pn); // then do some stuff which is common to all commands ... } } </code></pre> <p>but obviously, there's a problem here : the syntax <code>template &lt;typename R,typename T1, typename T2, ..., typename Tn&gt;</code> isn't legal C++ , AFAIK. </p> <p>Do I have to write n versions of Command, like :</p> <pre><code>template &lt;typename R&gt; class Command template &lt;typename R,typename T1&gt; class Command template &lt;typename R,typename T1, typename T2&gt; class Command ... </code></pre> <p>and so on ? (not even sure this is gonna work indeed) </p> <p>Or is there another, more elegant way to do this ? Is the syntax, mentioned <a href="https://stackoverflow.com/questions/3125950/c-weird-syntax-spotted-in-boost-template-parameters">here</a> of any use there ? (function f;)</p> <p>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 ?</p> <p>Any insights on this ? Regads, D. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload