如何使用std::map和boost::phoenix

How to use std::map with boost::phoenix?

本文关键字:boost phoenix map std 何使用      更新时间:2023-10-16

如何在phoenix lambda函数中使用std::map

#include <boostphoenix.hpp>
#include <map>
int main() {
    using namespace boost::phoenix;
    using namespace boost::phoenix::arg_names;
    using namespace std;
    map<int, int> m;
    auto foo = at(m, 3);
    foo();
}

为什么它不起作用?我得到以下错误:

C2440   'return': cannot convert from 'int' to 'std::pair<const _Kty,_Ty> ' xxx c:libboostphoenixstlcontainercontainer.hpp    167

我目前正在使用Visual Studio 2015社区和boost 1.60库。

基于jv_提出的问题

与其使用at函数,不如使用operator[]

#include <boost/phoenix.hpp>
#include <map>
int main() {
    std::map<int, int> m;
    m[3] = 33;
    auto foo = boost::phoenix::ref(m)[3];
    std::cout << foo() << "n";
}

phoenix at懒惰函数的实现使用value_type[1][2]来确定结果类型,在这种情况下是std::pair<const int,int>。然而,std::map<int,int>::at只是返回一个referenceconst_reference