Boost::list_of 无法对稍微复杂的数据结构进行模板推导
Boost::list_of not able to do template deduction for slightly complicated data structure
我必须使用跨平台代码。现在,我一直在使用 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
// I would have really liked the following:
// error:
MyData2 expData22 = std::make_tuple(10 , list_of(NodeInfo(1,2,3) (NodeInfo(3,4,5) )));
你只是把括号弄错了。以下应该有效:
MyData2 expData22 = std::make_tuple(10 , list_of(NodeInfo(1,2,3))(NodeInfo(3,4,5)));
的确,使这项工作顺利进行的最好方法是扩展库(或者编写自己的辅助函数)。
但是,您尝试的东西从来都不是功能。 documentation 展示了如何从 list_of
赋值给向量:
Tuple mytuple2 = std::make_tuple(10,
list_of(NodeInfo(1, 2, 3))(NodeInfo(3, 4, 5)).to_container(std::get<1>(mytuple2))
);
更多注释:
- 在我看来你忘了制作
expData3
和 expData4
容器。
- 有时您将
std::make_tuple(1,2,3)
用作 NodeInfo
。我不明白它应该如何工作
这是 fixes/workarounds 的完整列表:
#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));
}
我必须使用跨平台代码。现在,我一直在使用 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
// I would have really liked the following: // error: MyData2 expData22 = std::make_tuple(10 , list_of(NodeInfo(1,2,3) (NodeInfo(3,4,5) )));
你只是把括号弄错了。以下应该有效:
MyData2 expData22 = std::make_tuple(10 , list_of(NodeInfo(1,2,3))(NodeInfo(3,4,5)));
的确,使这项工作顺利进行的最好方法是扩展库(或者编写自己的辅助函数)。
但是,您尝试的东西从来都不是功能。 documentation 展示了如何从 list_of
赋值给向量:
Tuple mytuple2 = std::make_tuple(10,
list_of(NodeInfo(1, 2, 3))(NodeInfo(3, 4, 5)).to_container(std::get<1>(mytuple2))
);
更多注释:
- 在我看来你忘了制作
expData3
和expData4
容器。 - 有时您将
std::make_tuple(1,2,3)
用作NodeInfo
。我不明白它应该如何工作
这是 fixes/workarounds 的完整列表:
#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));
}