In C++, I 'd like to create a new element (shared_ptr) for each type specified in a typelist (and add the resulting pointers to a vector). In pseudo code, this should look similar to this:
vector< shared_ptr< baseT > > v;
foreach(type T: <derivedT1, derivedT2, ..., derivedTn>)
{
v.emplace_back(make_shared< T >());
}
Is there a concise solution for this using std or boost (MPL, Fusion, ?)?
Some Research:
- I found something similar in
type visitor over typelist in c++.
Unfortunately, I currently can't conceive a way how to replace the
sizeoffrom that post with the creation code from the example above and how to encapsulate the required definitions for concise usage. - In boost MPL and Fusion, I could not yet find a suitable iteration algorithm operating only on a type (without an instance of that type).
[Full disclosure: I develop Hana]
I'll answer using the Hana library, which is not yet in Boost but will be proposed for formal review soon. Hana merges the functionality of MPL and Fusion under a unified interface.