#include <iostream>
#include <vector>
#include <memory>

using namespace std;

struct BaseComponent
{
    template <typename T>
    T * as()
    {
        return dynamic_cast<T*>(this);
    }

    virtual ~BaseComponent() {}
};

template <typename T>
struct Component : public BaseComponent
{
    virtual ~Component() {}
};

struct PositionComponent : public Component<PositionComponent>
{
    float x, y, z;

    virtual ~PositionComponent() {}
};

struct AnotherComponent : public Component<AnotherComponent>
{
    virtual ~AnotherComponent () {}
};

int main()
{
    std::vector<std::unique_ptr<BaseComponent>> mComponents;
    mComponents.emplace_back(new AnotherComponent);

    auto *pos = mComponents[0]->as<PositionComponent>();
    if (pos) {
        pos->x = 1337;
    } else {
        cerr << "Not a PositionComponent." << endl;
    }

    return 0;
}