I have a base class Event, from which concrete events derive:
struct Event
{
};
struct CollisionEvent : Event
{
//CollisionEvent(Entity entityA, Entity entityB) : entityA(entityA), entityB(entityB) {}
Entity entityA;
Entity entityB;
};
this class is a POD, and I should be able to perform aggregate initializaion. But the following code fails to compile:
called from application:
eventBus->DispatchEvent<CollisionEvent>(mEntities[i], mEntities[j]);
in EventBus.h:
template <typename TEvent, typename... Args>
void DispatchEvent(Args&&... args)
{
auto it = mEventHandlers.find(typeid(TEvent));
if (it != mEventHandlers.end())
for (auto &&eventHandler : it->second)
eventHandler->Invoke(TEvent{ std::forward<Args>(args)... });
}
saying that it cannot convert initializer list to TEvent. Why?
For aggregate initialization you need to initialize the base class, even if it's empty.
eventBus->DispatchEvent<CollisionEvent>(Event{}, mEntities[i], mEntities[j]);
There were also some changes regarding what is and what isn't an aggregate in recent standards - until C++17 a class having a base class can't be aggregate-initialized, so you can't make it work in C++14. (see https://godbolt.org/z/Pq5PfecKq)