Search code examples
c++boostapache-phoenix

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


How can i use std::map inside phoenix lambda function?

#include <boost\phoenix.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();
}

Why it does not work? I get the following error:

C2440   'return': cannot convert from 'int' to 'std::pair<const _Kty,_Ty> ' xxx c:\lib\boost\phoenix\stl\container\container.hpp    167

I'm currently using Visual Studio 2015 Community and boost 1.60 library.


Solution

  • Based on the question pointed out by jv_:

    Rather than using the at function, use 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";
    }
    

    It appears that implementation of the phoenix at lazy function uses value_type [1] [2] to determine the result type, which is a std::pair<const int,int> in this case. However std::map<int,int>::at just returns a reference or const_reference.