讓我們考慮下面的代碼:如何在Boost.Phoenix中使用基於範圍的for_each?
#include <boost/phoenix.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> const small_ints {1, 2, 3, 4, 5};
std::vector<int> const big_ints {11, 12, 13, 14, 15};
namespace phoenix = boost::phoenix;
using namespace boost::phoenix;
using namespace boost::phoenix::placeholders;
std::vector<int>::const_iterator big_ints_it;
std::for_each(small_ints.cbegin(), small_ints.cend(),
for_(phoenix::ref(big_ints_it) = begin(phoenix::cref(big_ints)), phoenix::ref(big_ints_it) != end(phoenix::cref(big_ints)), ++phoenix::ref(big_ints_it))
[
std::cout << val('(') << arg1 << ',' << *phoenix::ref(big_ints_it) << "), "
]
);
}
它正常工作,通過從雙迭代產生的只是笛卡爾乘積對
(1,11), (1,12), (1,13), (1,14), (1,15), (2,11), (2,12), …, (5,14), (5,15),
的印刷期望的序列。
但是for_
是醜陋的!看看所有這些ref
,cref
(即不知何故,我總是必須明確地從phoenix
中選擇,否則會與std
版本發生衝突)。更何況,我必須保持無用big_ints_it
變量!
還要注意,這跟在example provided by Phoenix documentation之後。
現在我試圖用for_each
mentioned along iteration algorithms,期待更簡單的版本:
std::for_each(small_ints.cbegin(), small_ints.cend(),
for_each(phoenix::cref(big_ints),
std::cout << val('(') << arg1 << ", " << arg2 << "), "
)
);
,但它甚至不編譯!或者至少在Visual Studio 2013和Boost 1.61中。
我收到多個錯誤。第一個是
1>c:\programming\boost_1_61_0\boost\proto\detail\poly_function.hpp(205): error C2039: 'type' : is not a member of 'boost::proto::functional::at::result<Sig>'
1> with
1> [
1> Sig=boost::proto::functional::at (boost::phoenix::vector2<const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::phoenix::detail::tag::function_eval,boost::proto::argsns_::list3<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::impl::for_each>,0>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::reference_wrapper<const std::vector<int,std::allocator<int>>>>,0>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<std::basic_ostream<char,std::char_traits<char>> &>,0>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::argument<1>>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char [3]>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::argument<2>>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char [4]>,0>>>,2>>>,3>> *,const int &> &,boost::mpl::int_<2>)
1> ]
…
因爲StackOverflow上說,我甚至不能進入整個第一個錯誤,這個問題身體僅限於我已經進入他們的135034 30,000個字符...
哇!現在我讀了你的答案,我想知道我怎麼能沒有看到'arg2'的明顯問題!我在想什麼?!謝謝! –
哦!至於編譯器和實際lambda表達式,你顯然是對的。然而,對於我最後的工作,我用舊編譯器堆棧。雖然問題的例子,我正在做一個不同的編譯器。 –