#include <iostream>
#include <typeinfo>
using namespace std;
template <class T>
struct traitFoo: public T {
traitFoo():T(){}
virtual ~traitFoo(){}
virtual void foo()
{
cout << typeid(this).name() << " " << typeid(T).name() << " foo" << endl;
cout << this->i << endl; // Раскомментируйте для боли - всё ок
}
};
struct baseStruct {};
template <typename T>
struct withi : baseStruct {T i;};
struct bar : public withi<int>{};
struct baz : public withi<float>{};
int main() {
using BarFoo = traitFoo<bar>;
using BazFoo = traitFoo<baz>;
BarFoo rfoo;
rfoo.i = 1.5;
BazFoo zfoo;
zfoo.i = 1.5;
rfoo.foo();
zfoo.foo();
// Как сюда дабавить хранение BarFoo и BazFoo в общем массиве? - вроде норм
baseStruct arr[2] = { rfoo, zfoo };
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZWluZm8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKdGVtcGxhdGUgPGNsYXNzIFQ+CnN0cnVjdCB0cmFpdEZvbzogcHVibGljIFQgewoJdHJhaXRGb28oKTpUKCl7fQoJdmlydHVhbCB+dHJhaXRGb28oKXt9Cgl2aXJ0dWFsIHZvaWQgZm9vKCkKCXsgCgkJY291dCA8PCB0eXBlaWQodGhpcykubmFtZSgpIDw8ICIgIiA8PCB0eXBlaWQoVCkubmFtZSgpIDw8ICIgZm9vIiA8PCBlbmRsOwoJCWNvdXQgPDwgdGhpcy0+aSA8PCBlbmRsOyAvLyDQoNCw0YHQutC+0LzQvNC10L3RgtC40YDRg9C50YLQtSDQtNC70Y8g0LHQvtC70LggLSDQstGB0ZEg0L7QugoJfQp9OwoKc3RydWN0IGJhc2VTdHJ1Y3Qge307CnRlbXBsYXRlIDx0eXBlbmFtZSBUPgpzdHJ1Y3Qgd2l0aGkgOiBiYXNlU3RydWN0IHtUIGk7fTsKc3RydWN0IGJhciA6IHB1YmxpYyB3aXRoaTxpbnQ+e307CnN0cnVjdCBiYXogOiBwdWJsaWMgd2l0aGk8ZmxvYXQ+e307Cg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