Search code examples
c++boostboost-phoenix

Motivation to use boost::phoenix::val?


Please, could you show situations, where the use of boost::phoenix::val is indispensable (or at least very handy), not just optional. Boost doc doesn't go far beyond examples like

std::cout<<boost::phoenix::val(3)();   //output is 3.

When and why one would prefer

boost::phoenix::val(t)();

instead of just

t;

Solution

  • You wouldn't.

    You'd prefer it if you required a (lazy) callable:

    template <typename F>
    void print_three_times(F f) {
        std::cout << 3*f() << "\n";
    }
    

    Now you can call it using

    print_three_times(phx::val(3));
    
    int i;
    std::cin >> i;
    print_three_times(phx::val(i));
    

    Also you can use it to coerce any reference to a phoenix lazy actor:

    std::cout << 3; // not an actor
    std::cout << val(3); // a lazy actor