gpt4 book ai didi

c++ - Boost::list_of 无法对稍微复杂的数据结构进行模板推导

转载 作者:行者123 更新时间:2023-11-30 03:55:25 25 4
gpt4 key购买 nike

我必须使用跨平台代码。现在,我一直在使用 VS2012,它不支持 C++11 统一初始化。所以,我正在使用 boost::list_of

对于简单的情况,这是可行的。但是,对于稍微复杂的数据结构,这似乎行不通。

#include <iostream>
#include <boost/assign.hpp>
#include <boost/assign/list_of.hpp>
#include <tuple>
using namespace std;
using namespace boost::assign;

int main() {

class NodeInfo
{
public:
NodeInfo (int Parent, int childLeft, int childRight) :
m_parent (Parent), m_childLeft(childLeft), m_childRight(childRight)
{

}
private:
int m_parent;
int m_childLeft;
int m_childRight;
};
typedef std::vector<NodeInfo> MyData1;
MyData1 expData = list_of(NodeInfo(1,2,3)) (NodeInfo(3,4,5));

typedef std::tuple<int , std::vector<NodeInfo>> MyData2;
MyData2 expData21 = std::make_tuple(10 , expData);

// I would have really liked the following:
// error:
MyData2 expData22 = std::make_tuple(10 , list_of(NodeInfo(1,2,3) (NodeInfo(3,4,5) )));

//error:From the example in boost doc, this should work.
MyData2 expData3 = list_of<MyData2> (std::make_tuple(10 , expData));

MyData2 expData4;
// error: Most probably need to write my own list_inserter for MyTest,
// after turning MyTest into a class.
// I really don't want to go this route, if the other options are at all
// possible. Why extend the boost library, if it's at all possible ?
insert( expData4 )
( std::make_tuple(10 , expData) )( std::make_tuple(20 , expData) );

return 0;
}

我在这里添加了部分代码。 https://ideone.com/kIYDu0

最佳答案

确实,使这项工作顺利进行的最佳方法是扩展库(或者编写您自己的辅助函数)。

但是,您尝试的东西从来都不是功能。 documentation显示如何将 list_of 分配给 vector :

Tuple mytuple2 = std::make_tuple(10, 
list_of(NodeInfo(1, 2, 3))(NodeInfo(3, 4, 5)).to_container(std::get<1>(mytuple2))
);

更多注意事项:

  • 在我看来,您忘记了将 expData3expData4 设为容器。
  • 在某些时候,您使用 std::make_tuple(1,2,3) 作为 NodeInfo。我不明白那应该如何运作

这是包含修复/解决方法的完整列表:

Live On Coliru

#include <iostream>
#include <boost/assign.hpp>
#include <boost/assign/list_of.hpp>
#include <tuple>
using namespace std;
using namespace boost::assign;

int main() {

class NodeInfo {
public:
NodeInfo(int Parent, int childLeft, int childRight)
: m_parent(Parent), m_childLeft(childLeft), m_childRight(childRight) {}

private:
int m_parent;
int m_childLeft;
int m_childRight;
};

typedef std::vector<NodeInfo> NodeInfos;
NodeInfo node1(1,2,3), node2(3,4,5);
NodeInfos infos = list_of(node1)(node2);

typedef std::tuple<int, std::vector<NodeInfo> > Tuple;
Tuple mytuple = std::make_tuple(10, infos);

// I would have really liked the following:
Tuple mytuple2 = std::make_tuple(10,
list_of(NodeInfo(1, 2, 3))(NodeInfo(3, 4, 5)).to_container(std::get<1>(mytuple2))
);

std::vector<Tuple>
mytuples = list_of<Tuple>(std::make_tuple(10, infos)),
mytuples2;

//// still figuring this out:
// insert(mytuples2)(Tuple(10, infos))(Tuple(20, infos));
}

关于c++ - Boost::list_of 无法对稍微复杂的数据结构进行模板推导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29050719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com