尝试将id映射到包含任意数据的元组:
typedef boost::tuples::tuple<std::string, std::string, unsigned char> data;
typedef std::map<uint64, data> data_map;
const data_map DATA = boost::assign::map_list_of
(0, boost::tuples::make_tuple("XXX", "xxx", 0))
(0, boost::tuples::make_tuple("YYY", "yyy", 1))
(0, boost::tuples::make_tuple("ZZZ", "zzz", 2))
如何访问DATA地图中的元组?我正在尝试:
data_map::const_iterator itData = DATA.find(id);
然后用以下方式访问它(在检查是否找到之后):
data metadata = itData->second;
引发错误:
error: invalid initialization of reference of type 'boost::tuples::cons<const char (&)[12], boost::tuples::cons<int, boost::tuples::null_type> >::stored_head_type {aka const char (&)[12]}' from expression of type 'const char [11]'
答案 0 :(得分:4)
参与我的水晶球:
我认为您使用了decltype(obj)
,其中object是std::make_tuple("abcdefghijk",...)
或std::tie("abcdefghij",...)
的结果。
这是因为错误消息讨论了这种类型的元组元素(const char (&)[12]
)。
证明上述代码没问题:
<强> Live On Coliru 强>
#include <boost/assign.hpp>
#include <boost/tuple/tuple.hpp>
#include <map>
typedef boost::tuples::tuple<std::string, std::string, unsigned char> data;
typedef std::map<uint64_t, data> data_map;
int main() {
const data_map DATA = boost::assign::map_list_of
(0, boost::tuples::make_tuple("XXX", "xxx", 0))
(1, boost::tuples::make_tuple("YYY", "yyy", 1))
(2, boost::tuples::make_tuple("ZZZ", "zzz", 2));
data_map::const_iterator itData = DATA.find(2);
data metadata = itData->second;
}
答案 1 :(得分:0)
尝试使用:
string("xxx")
而不是:
"xxx"
对于make_tuple调用中的每个字符串。
答案 2 :(得分:0)
在创建直接工作的boost::tuples::make_tuple
元组替换data
时:
typedef boost::tuples::tuple<std::string, std::string, unsigned char> data;
const data_map DATA = boost::assign::map_list_of
(0, data("XXX", "xxx", 0))
(1, data("YYY", "yyy", 1))
(2, data("ZZZ", "zzz", 2))