有时候我需要使用用户类型的一元素集合(或任何其他容器)并通过以下方式创建它们:
boost::assign::list_of(typeVariable).convert_to_container<std::unordered_set<UserType> >()
有没有更加优美的方式呢?
PS: 例如,我们有一个业务逻辑API,它搜索元素,并且需要选择一组类型用于筛选。不同的用户可以访问不同的类型。此外,我们可以选择任何一种类型进行过滤,这种情况下,我们将从过滤选项中选择这一种类型。
因此,我只想要用一行简单的代码来实现。我的当前版本是:
getElements(filter.type != UNDEFINED
? boost::assign::list_of(filter.type).convert_to_container<std::set<UserType> >()
: std::set<UserType>(allowedTypes.begin(), allowedTypes.end()))