Saving the old question. See below for permission. This is probably something simple, but still. I have the following C ++ 11 code snippet:
#include <vector> template <typename... Ts> struct typelist { }; template <typename T> struct EventContainer { typedef T Type; /// TODO. Ring buffer std::vector<T> container; void push(const T& t) { EventContainer<T>::container.push_back(t); } virtual ~EventContainer() { } }; template <template <typename...> class TL> class EventStorage: public EventContainer<Ts>... { }; class Event1 { }; class Event2 { }; typedef typelist<Event1,Event2> Events12; int main() { EventStorage<Events12> ev; return 0; }
How can I make an EventStorage
inherit an EventContainer
bookmark each type in a typelist
. I could do this using the Loki :: library, but I want to use C ++ 11 with variable templates. Thanks.
Resolution 1: Fix EventStorage
template EventStorage
. This will make EventStorage
somewhat inherit all EventContainer
templates with each Ts
type.
template <typename...> class EventStorage { }; template <typename... Ts> class EventStorage < typelist<Ts...> >: public EventContainer<Ts>... { };
Now I have a compile time error, the following main()
:
int main() { EventStorage<Events12> ev; Event1 ev1; ev.push(ev1); return 0; } In function 'int main()': error: request for member 'push' is ambiguous error: candidates are: void EventContainer<T>::push(const T&) [with T = Event2] error: void EventContainer<T>::push(const T&) [with T = Event1]
Why is the compiler confusing? In the end, I click on a specific type. GCC 4.6.1 here.
resolution2: Since @Matthieu M. suggested that I introduce an int EventStorage
forwarding method, but with one extra call to functin:
template <typename T> void push(const T& t) { EventContainer<T>::push(t); }
According to Alexandrescu, the compiler optimizes this direct call while the parameters are references. Now the question is officially closed :)
source share