gpt4 book ai didi

c++ - 带有 boost::zip_iterator 的 std::for_each 编译失败

转载 作者:行者123 更新时间:2023-11-28 06:12:52 31 4
gpt4 key购买 nike

以下代码无法为我编译:

#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 的错误消息:

In file included from /usr/include/c++/4.9/algorithm:62:0, from /usr/include/boost/utility/swap.hpp:24, from /usr/include/boost/tuple/detail/tuple_basic.hpp:40, from /usr/include/boost/tuple/tuple.hpp:33, from /usr/include/boost/iterator/zip_iterator.hpp:19, from prog.cpp:4: /usr/include/c++/4.9/bits/stl_algo.h: In instantiation of '_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:
required from here /usr/include/c++/4.9/bits/stl_algo.h:3755:14: error: no match for call to '(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: note: candidate is: struct zip_func : ^ prog.cpp:12:8: note: void zip_func::operator()(EntryTuple&) const void operator()(EntryTuple& t) const ^ prog.cpp:12:8: note: no known conversion for argument 1 from '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 >}' to 'EntryTuple& {aka boost::tuples::tuple&}'

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

这是什么原因?

最佳答案

GCC 5.1 produces a more understandable error message:

/usr/local/include/c++/5.1.0/bits/stl_algo.h:3767:5: error: invalid initialization of non-const reference of type 'EntryTuple& {aka boost::tuples::tuple&}' from an rvalue of type '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

关于c++ - 带有 boost::zip_iterator 的 std::for_each 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30894830/

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