c++ - Boost 为结构分配 list_of

标签 c++ boost stl

我有一个关于 boost::assign_list 的问题

struct AClass{
AClass();
};

struct BClass{
BClass();
};

typedef AInfo{
   string infoname;
   AClass m_nAClass;
   BClass m_nBClass;
};

typedef list<AInfo> listOfAInfo;

listOfAInfo m_mlistOfAInfo =
     boost::assign::list_of("AInfoName1", AClass(), BClass() );

如何为结构初始化 map_lists 数组。这是遗留代码的一个版本,其中一些类被定义为具有构造函数的结构。

谢谢, 开发包

最佳答案

我不确定“其中一些类被定义为具有构造函数的结构” 是什么意思。

我看到的是聚合,即使在 C++03(或 C++98 IIRC)中也可以使用聚合初始化语法进行初始化:

AInfo obj = { "AInfoName1", AClass(), BClass() };

也许你可以使用

typedef std::map<int, AInfo> listOfAInfo;

listOfAInfo m_mlistOfAInfo =
    boost::assign::map_list_of
        ( 1, { "AInfoName1", AClass(), BClass() })
        ( 2, { "AInfoName2", AClass(), BClass() });

typedef std::list<AInfo> listOfAInfo;

listOfAInfo m_mlistOfAInfo =
    boost::assign::list_of
        ( AInfo { "AInfoName1", AClass(), BClass() })
        ( AInfo { "AInfoName2", AClass(), BClass() });

这里是 Live On Coliru (c++11)。

似乎您不能使用统一初始化语法 (AInfo {...}),它与聚合初始值设定项 = { ... } 略有不同) 和 Boost Assign,因为编译器不知道你在初始化什么,这与上面显示的 obj 声明不同。

关于c++ - Boost 为结构分配 list_of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858761/

相关文章:

C++ 绘制 Mandelbrot 集,性能不佳

c++ - 为什么 std::count 和 std::find 没有优化为使用 memchr?

c++ - 在 boost::asio 中是否可以有一个具有多个执行时间的 deadline_timer?

c++ - 如何获取 vector 的那些元素,其中另一个 vector 在相应索引处具有 '1' s

c++ - 在 C++ 中将字符串转换为数字

c++ - double 随机数数组

C++ Cmake boost lib不断抛出很多错误

boost - c++0x lambdas,不让我作为函数 ptr 传递

c++ - STL 算法 copy if with functor

c# - 如何为 Windows 7/8 实现自动播放