Hello friends, i have the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
struct Foo
{
virtual void Pure() = 0;
};
template <typename T>
struct Bar : Foo
{
virtual void Pure() override
{
}
};
struct Baz
{
Baz(std::initializer_list<Foo> foos){}
};
int main()
{
Baz
{
Bar<int>{},
Bar<float>{}
};
return 0;
}
|
Baz needs to be constructed with a list of child classes of Foo, which is an abstract class. The problem is, apparently, initializer lists (& other list containers i've checked such as vector) cannot hold abstract classes.
I've tried changing the type from Foo to Foo&, however as it turns out containers cant hold references either.
I tried using std::reference_wrapper instead, which the initializer_list doesn't complain about, however then compiler complains that there are "no matching function call to Baz::Baz(<brace-enclosed initializer list>)"
i'm guessing its because the std::reference_wrapper cannot be constructed from the rvalues that im passing? So it cannot convert the initializer list of Foos to initializer list of reference_wrapper<Foo>.
So my question is what do?
The only solution i can think of is to make the initializer_list of type Foo* and then doing
new Bar<int>{}
instead (or using smart pointers), however i dont like this as it seems to be needless, i should be able to do it without heap allocation, shouldn't i?
thanks & thanks