Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11 variadic template template parameters
    primarykey
    data
    text
    <p>Keeping the old question. See below for resolution. It is probably something simple, but still. I have the following C++11 code fragment:</p> <pre><code>#include &lt;vector&gt; template &lt;typename... Ts&gt; struct typelist { }; template &lt;typename T&gt; struct EventContainer { typedef T Type; /// TODO. Ring buffer std::vector&lt;T&gt; container; void push(const T&amp; t) { EventContainer&lt;T&gt;::container.push_back(t); } virtual ~EventContainer() { } }; template &lt;template &lt;typename...&gt; class TL&gt; class EventStorage: public EventContainer&lt;Ts&gt;... { }; class Event1 { }; class Event2 { }; typedef typelist&lt;Event1,Event2&gt; Events12; int main() { EventStorage&lt;Events12&gt; ev; return 0; } </code></pre> <p>How can I make <code>EventStorage</code> inherit <code>EventContainer</code> templeted with each of the types in the <code>typelist</code>. I could do it with Loki:: library, but I want to use C++11 with variadic templates. Thank you.</p> <p>Resolution1: Fixing <code>EventStorage</code> template template issue. This will make <code>EventStorage</code>, multiple inherit all <code>EventContainer</code> templated with each type of <code>Ts</code>.</p> <pre><code>template &lt;typename...&gt; class EventStorage { }; template &lt;typename... Ts&gt; class EventStorage &lt; typelist&lt;Ts...&gt; &gt;: public EventContainer&lt;Ts&gt;... { }; </code></pre> <p>Now I have compile time error, on the following <code>main()</code>:</p> <pre><code>int main() { EventStorage&lt;Events12&gt; ev; Event1 ev1; ev.push(ev1); return 0; } In function ‘int main()’: error: request for member ‘push’ is ambiguous error: candidates are: void EventContainer&lt;T&gt;::push(const T&amp;) [with T = Event2] error: void EventContainer&lt;T&gt;::push(const T&amp;) [with T = Event1] </code></pre> <p>Why the compiler is confused? After all I push with specific type. GCC 4.6.1 here.</p> <p>Resolution2: As @Matthieu M. suggested I can present a forwarding method int <code>EventStorage</code>, but at a cost of one extra functin call:</p> <pre><code>template &lt;typename T&gt; void push(const T&amp; t) { EventContainer&lt;T&gt;::push(t); } </code></pre> <p>According to Alexandrescu, the compiler will optimize this forward call as long as parameters are references. Now the question is officially closed :)</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