I would like to do something similar to this:
#include <list>
#include <iostream>
#include <functional>
class Abstract {
public:
virtual void foo() = 0;
};
class Concrete : public Abstract {
void foo() {
std::cout << "foo's implementation" << std::endl;
}
};
typedef std::list<Abstract> abstract_list;
class Someclass {
public:
Someclass(abstract_list list) : m_list(list) {}
private:
abstract_list m_list;
};
int main(int argc, char **argv) {
Someclass l({ Concrete(), Concrete() });
return 0;
}
Now, I know I can't. Abstract being incomplete, it cannot be used as is in the container. I don't want to use pointers (raw ppointer or unique_ptr).
I try to rewrite my type as:
typedef std::list<std::reference_wrapper<Abstract> > abstract_list;
But it fails with
error: use of deleted function ‘std::reference_wrapper<_Tp>::reference_wrapper(_Tp&&) [with _Tp = Abstract]’
Because std::reference_wrapper do not work with rvalue.
Is there any other option to statically allocate a list containing abstract entities using an elegant braced initializer list ?