#include <iostream>
#include <vector>
#include <memory>
#include <random>
#include <array>
struct a
{
virtual ~a() = default;
};
struct b : public a {};
struct c : public a {};
using pointer_type = std::unique_ptr<a>;
using vector_type = std::vector<pointer_type>;
pointer_type random_class()
{
static std::mt19937 gen((std::random_device())());
std::uniform_int_distribution<unsigned> dist(0, 2);
switch (dist(gen))
{
case 0: return pointer_type(new a);
case 1: return pointer_type(new b);
case 2: return pointer_type(new c);
}
}
const char* identity(const a* object)
{
if (const c* o = dynamic_cast<const c*>(object))
return "c";
if (const b* o = dynamic_cast<const b*>(object))
return "b";
return "a";
}
template<typename iter_type>
void display(iter_type beg, iter_type end)
{
std::cout << "{ ";
while (beg != end)
std::cout << identity((beg++)->get()) << ' ';
std::cout << "}\n";
}
int main()
{
vector_type v;
for (unsigned i = 0; i < 10; ++i)
v.emplace_back(random_class());
display(v.begin(), v.end());
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWVtb3J5PgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8YXJyYXk+CgoKc3RydWN0IGEKewogICAgdmlydHVhbCB+YSgpID0gZGVmYXVsdDsKfTsKCnN0cnVjdCBiIDogcHVibGljIGEge307CnN0cnVjdCBjIDogcHVibGljIGEge307CgoKdXNpbmcgcG9pbnRlcl90eXBlID0gc3RkOjp1bmlxdWVfcHRyPGE+Owp1c2luZyB2ZWN0b3JfdHlwZSA9IHN0ZDo6dmVjdG9yPHBvaW50ZXJfdHlwZT47Cgpwb2ludGVyX3R5cGUgcmFuZG9tX2NsYXNzKCkKewogICAgc3RhdGljIHN0ZDo6bXQxOTkzNyBnZW4oKHN0ZDo6cmFuZG9tX2RldmljZSgpKSgpKTsKICAgIHN0ZDo6dW5pZm9ybV9pbnRfZGlzdHJpYnV0aW9uPHVuc2lnbmVkPiBkaXN0KDAsIDIpOwoKICAgIHN3aXRjaCAoZGlzdChnZW4pKQogICAgewogICAgY2FzZSAwO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