如何将boost :: assign与扩展STL容器的自定义容器一起使用?

斯蒂芬·D

为了使自定义类(继承自STL容器(如std::vectorstd::map))与boost::assign list_of()map_list_of()初始化函数一起工作,我该怎么做?

背景

我想轻松地用值列表初始化容器。C ++ 11引入了初始值设定项列表,但是我被C ++ 03所困扰,所以我不能使用C ++ 11初始值设定项列表

作为一种解决方法,我发现了boost:assign库,它提供了诸如list_of()和map_list_of()之类的功能。这对于像std :: vector和std :: map这样的STL容器来说效果很好。但是,如果我通过扩展std :: vector创建自己的容器,则会出现编译错误。

这是一个小例子

#include "boost/assign/list_of.hpp"
using namespace boost::assign;
#include <vector>

struct SpecialVector : public std::vector<int>{
    foo(){/* adds functionality */}
};

int main(){
    std::vector<int> v = list_of(1)(2)(3); // list_of() works well for STL containers

    // The following works but requires adding items one-by-one with push_back
    SpecialVector u; 
    u.push_back(1);
    u.push_back(2);
    u.push_back(3);

    // The following fails when attempting to compile
    SpecialVector u2 = list_of(1)(2)(3);
}

尝试编译该示例会给我以下错误:

In file included from assign_inherited.cpp:1:0:
../../../lib/boost/assign/list_of.hpp: In instantiation of 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert(const Container*, boost::assign_detail::default_type_tag) const [with Container = SpecialVector; DerivedTAssign = boost::assign_detail::generic_list<int>; Iterator = std::_Deque_iterator<int, int&, int*>]':
../../../lib/boost/assign/list_of.hpp:142:38:   required from 'Container boost::assign_detail::converter<DerivedTAssign, Iterator>::convert_to_container() const [with Container = SpecialVector; DerivedTAssign = boost::assign_detail::generic_list<int>; Iterator = std::_Deque_iterator<int, int&, int*>]'
../../../lib/boost/assign/list_of.hpp:436:81:   required from 'boost::assign_detail::generic_list<T>::operator Container() const [with Container = SpecialVector; T = int]'
assign_inherited.cpp:19:39:   required from here
../../../lib/boost/assign/list_of.hpp:163:20: error: no matching function for call to 'SpecialVector::SpecialVector(boost::assign_detail::converter<boost::assign_detail::generic_list<int>, std::_Deque_iterator<int, int&, int*> >::iterator, boost::assign_detail::converter<boost::assign_detail::generic_list<int>, std::_Deque_iterator<int, int&, int*> >::iterator)'
             return Container( begin(), end() );
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
assign_inherited.cpp:5:8: note: candidate: SpecialVector::SpecialVector()
 struct SpecialVector : public std::vector<int>{
        ^~~~~~~~~~~~~
assign_inherited.cpp:5:8: note:   candidate expects 0 arguments, 2 provided
assign_inherited.cpp:5:8: note: candidate: SpecialVector::SpecialVector(const SpecialVector&)
assign_inherited.cpp:5:8: note:   candidate expects 1 argument, 2 provided

我已经检查了boost :: assign库的文档。我找到了“扩展库”部分,但是,如果我对它的理解正确,那么本部分将把自定义类作为列表中的项添加,而不是为自定义类生成初始化器还是我理解错了?

看到

就像您说的那样,您需要允许从基本类型进行构造:

Live On Coliru

#include "boost/assign/list_of.hpp"
using namespace boost::assign;
#include <vector>

struct SpecialVector : std::vector<int>{
    typedef std::vector<int> base;
    void foo(){/* adds functionality */}

    SpecialVector() : base() {}
    template <typename T> explicit SpecialVector(T const& t) : base(t) {}
    template <typename T, typename U> SpecialVector(T const& t, U const& u) : base(t, u) {}
    template <typename T, typename U, typename V> SpecialVector(T const& t, U const& u, V const& v) : base(t, u, v) {}
};

int main(){
    std::vector<int> v = list_of(1)(2)(3); // list_of() works well for STL containers

    // The following works but requires adding items one-by-one with push_back
    SpecialVector u; 
    u.push_back(1);
    u.push_back(2);
    u.push_back(3);

    // The following fails when attempting to compile
    SpecialVector u2 = list_of(1)(2)(3);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将 std::ranges 算法与自定义容器和迭代器一起使用

如何在自定义网络中将 JUnit 5 与测试容器一起使用?

如何将 scoped_allocator_adaptor 与自定义分配器(包装在一个类中)一起使用,以便它可以为某些类型而不是 STL 容器解包?

如何:将异步方法与LINQ自定义扩展方法一起使用

如何将Boost.Python中的map_indexing_suite与自定义的非std对象一起使用?

如何将文件传递到Docker容器中以与该容器一起使用?

如何将Spring REST Projections与自定义控制器一起使用

如何将 ConcurrentWebSocketSessionDecorator 与自定义 WebSocketHandler 一起使用

如何将 Firebase 存储与自定义服务器一起使用?

如何将Picasso与用于RecyclerView的自定义适配器一起使用

如何将sklearn Pipeline与自定义功能一起使用?

如何将AWS Cognito与自定义NodeJS服务器一起使用?

如何将dask.dataframe与自定义dsk图一起使用

如何将 lodash sortBy 与自定义订单比较器一起使用

PS7.1-如何将管道链接与自定义功能一起使用?

如何将标准验证数据注释与自定义类型一起使用?

如何将Alamofire与自定义标头一起使用

如何将AngularJS自定义元素与玉一起使用?

如何将azure流量管理与自定义服务url端点一起使用?

如何将MultiDex与自定义Application类一起使用?

如何将字段集与自定义表单一起使用?

如何将Google Cloud Functions与自定义Google Cloud端点一起使用?

如何将unordered_set与自定义结构一起使用?

如何将buildroot与软件包中的自定义更改一起使用

如何将 CMS 与我的自定义网站一起使用

如何将 UserManager 与自定义用户一起使用?

如何将Boost与Eclipse结合使用?

将标识与自定义属性一起使用

将咖啡与自定义EditText一起使用