【问题标题】:std::for_each with boost::zip_iterator fails to compile带有 boost::zip_iterator 的 std::for_each 无法编译
【发布时间】:2015-06-17 14:40:01
【问题描述】:

以下代码无法为我编译:

#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: 在 '_Funct 的实例化中 std::for_each(_IIter, _IIter, _Funct) [with _IIter = boost::zip_iterator >, __gnu_cxx::__normal_iterator >, 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, boost::tuples::null_type> >; _Funct = zip_func]': prog.cpp:34:1:
这里需要/usr/include/c++/4.9/bits/stl_algo.h:3755:14: 错误:不匹配调用 '(zip_func) (boost::iterator_facade >, __gnu_cxx::__normal_iterator >, 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, boost::tuples::null_type> >, boost::tuples::cons >, boost::random_access_traversal_tag, boost::tuples::cons >, int>::reference)' __f(*__first); ^ prog.cpp:9:8: 注意:候选是:struct zip_func: ^ prog.cpp:12:8: 注意:void zip_func::operator()(EntryTuple&) const void operator()(EntryTuple& t) const ^ prog.cpp:12:8: 注意:参数 1 的未知转换来自 'boost::iterator_facade >, __gnu_cxx::__normal_iterator >, 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, boost::tuples::null_type> >, boost::tuples::cons >, boost::random_access_traversal_tag, boost::tuples::cons >, int>::reference {aka boost::tuples::cons >}' 到 'EntryTuple& {aka boost::tuples::tuple&}'

如果我添加一些consts,它是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;
  }
};

是什么原因?

【问题讨论】:

  • Boost 的 zip 迭代器是仅查看的迭代器?顺便说一句,std::unary_function 是你们中的 C++03。

标签: c++ boost


【解决方案1】:

GCC 5.1 produces a more understandable error message:

/usr/local/include/c++/5.1.0/bits/stl_algo.h:3767:5:错误:无效 'EntryTuple& {aka 类型的非常量引用的初始化 boost::tuples::tuple&}' 来自类型的右值 'EntryTuple {aka boost::tuples::tuple}'

所以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;
  }
};

现场示例:https://ideone.com/hwC3bb

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多