以下代码无法为我编译:
#include <iostream>
#include <vector>
#include <boost/iterator/zip_iterator.hpp>
typedef boost::tuple<int&, float&> EntryTuple;
struct zip_func :
public std::unary_function<EntryTuple&, void>
{
void operator()(EntryTuple& t) const
{
std::cout << t.get<0>() << " " << t.get<1>() << std::endl;
}
};
int main()
{
const int N = 5;
std::vector<int> intVec(N,2);
std::vector<float> valueVec(N,5.5);
std::for_each(
boost::make_zip_iterator(
boost::make_tuple(intVec.begin(), valueVec.begin())
),
boost::make_zip_iterator(
boost::make_tuple(intVec.end(), valueVec.end())
),
zip_func()
);
return 0;
}
来自live example的错误消息:
在/usr/include/c++/4.9/algorithm:62:0中包含的文件中, 来自/usr/include/boost/utility/swap.hpp:24, 来自/usr/include/boost/tuple/detail/tuple_basic.hpp:40, 来自/usr/include/boost/tuple/tuple.hpp:33, 来自/usr/include/boost/iterator/zip_iterator.hpp:19, 来自prog.cpp:4:/usr/include/c++/4.9/bits/stl_algo.h:实例化&#39; _Funct std :: for_each(_IIter,_IIter,_Funct)[with _IIter = boost :: zip_iterator&gt;,__ gn_cxx :: __ normal_iterator&gt ;, boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, 升压::元组:: null_type&GT;取代; _Funct = zip_func]&#39;:prog.cpp:34:1:
从这里需要/usr/include/c++/4.9/bits/stl_algo.h:3755:14: 错误:无法匹配&#39;(zip_func) (boost :: iterator_facade&gt;,__ gn_cxx :: __ normal_iterator&gt ;, boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, 升压::元组:: null_type&GT; &gt;,boost :: tuples :: cons&gt;, boost :: random_access_traversal_tag,boost :: tuples :: cons&gt;, INT&GT; ::参考)&#39; __f(* __第一); ^ prog.cpp:9:8:注意:候选者是:struct zip_func: ^ prog.cpp:12:8:注意:void zip_func :: operator()(EntryTuple&amp;)const void operator()(EntryTuple&amp; t)const ^ prog.cpp:12:8:注意:参数1没有已知的转换 &#39; boost :: iterator_facade&gt ;, __gnu_cxx :: __ normal_iterator&gt ;, boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, boost :: tuples :: null_type,boost :: tuples :: null_type, 升压::元组:: null_type&GT; &gt;,boost :: tuples :: cons&gt;, boost :: random_access_traversal_tag,boost :: tuples :: cons&gt;, int&gt; :: reference {aka boost :: tuples :: cons&gt;}&#39;至 &#39;&EntryTuple放大器; {aka boost :: tuples :: tuple&amp;}&#39;
如果我添加了一些const
,则compiles:
typedef boost::tuple<const int&, const float&> EntryTuple;
struct zip_func :
public std::unary_function<const EntryTuple&, void>
{
void operator()(const EntryTuple& t) const
{
std::cout << t.get<0>() << " " << t.get<1>() << std::endl;
}
};
是什么原因?
答案 0 :(得分:1)
GCC 5.1 produces a more understandable error message:
/ usr / local / include / c ++ / 5.1.0 / bits / stl_algo.h:3767:5:错误:无效 初始化类型&#39; EntryTuple&amp;的非const引用。 {又名 提高::元组::元组放大器;}&#39;从一个类型的右值 &#39; EntryTuple {aka boost :: tuples :: tuple}&#39;
所以const
对于运算符的论证来说是必要的:
typedef boost::tuple<int&, float&> EntryTuple;
struct zip_func :
public std::unary_function<const EntryTuple&, void>
{
void operator()(const EntryTuple& t) const
{
t.get<0>() = 10;
std::cout << t.get<0>() << " " << t.get<1>() << std::endl;
}
};