将 boost::mpl::list 应用于类型的模板参数
我有一个需要 boost::variant 的类,其中包含指向各种类型的共享指针,如下所示:
template <typename ToySharedPtrVariant, typename ColorSharedPtrVariant>
class ToyPicker {
typedef std::pair<
ToySharedPtrVariant,
ColorSharedPtrVariant
> toyAndColorPair;
typedef std::map<
std::string,
std::vector<
toyAndColoPair
>
> stringToToyColorPairMap;
// ... methods that use the defined types...
}
该类当前需要以下形式的模板参数进行编译:
ToyPicker<
boost::variant<
boost::shared_ptr<ToyModel>
>,
boost::variant<
boost::shared_ptr<BlueToy>,
boost::shared_ptr<RedToy>,
boost::shared_ptr<GreenToy>
>
> toyPicker;
如何使用 mpl 列表,以便可以允许以下更简单的定义对于用户,然后在我的类实现中将其转换为上面的示例格式?
ToyPicker<
boost::mpl::list<
ToyModel
>,
boost::mpl::list<
BlueToy,
RedToy,
GreenToy
>
> toyPicker;
I have a class that requires a boost::variant containing shared pointers to various types as follows:
template <typename ToySharedPtrVariant, typename ColorSharedPtrVariant>
class ToyPicker {
typedef std::pair<
ToySharedPtrVariant,
ColorSharedPtrVariant
> toyAndColorPair;
typedef std::map<
std::string,
std::vector<
toyAndColoPair
>
> stringToToyColorPairMap;
// ... methods that use the defined types...
}
This class currently requires template parameters of the following form to compile:
ToyPicker<
boost::variant<
boost::shared_ptr<ToyModel>
>,
boost::variant<
boost::shared_ptr<BlueToy>,
boost::shared_ptr<RedToy>,
boost::shared_ptr<GreenToy>
>
> toyPicker;
How do I use an mpl list so that I can allow the following much simpler definition for users, then convert it into the example format above inside my class implementation?
ToyPicker<
boost::mpl::list<
ToyModel
>,
boost::mpl::list<
BlueToy,
RedToy,
GreenToy
>
> toyPicker;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 boost::mpl::transform 与 boost::make_variant_over 结合使用窍门:
Using boost::mpl::transform in conjunction with boost::make_variant_over does the trick :
看看 boost::make_variant_over 它做了什么你需要。
Look at boost::make_variant_over it does what you need.