Boost logo

Boost Users :

From: EMalenfant_at_[hidden]
Date: 2006-01-27 13:11:22


Hello,

Given the following:

    std::string Cat(const std::string& First, const std::string& Second)
    {
        return First + Second;
    }
    
    void Print(const char* S)
    {
        std::cout << S << std::endl;
    }
    
    std::string foo("foo");
    std::string bar("bar");
    

The novice Lambda user that I am would have thought that this:

    using namespace boost::lambda;
    
    bind(Print,
        bind(&std::string::c_str,
            bind(Cat, _1, _2)
        )
    )(foo, bar);
    
would be roughly equivalent to:

    Print(Cat(foo, bar).c_str());
    

But it seems to behave more like:

    const char* ptr_to_temp = Cat(foo, bar).c_str();
    Print(ptr_to_temp);
    
and invokes undefined behavior.

This workaround seems to work:

    var_type<std::string>::type temp(var(std::string()));

    (
        temp = bind(Cat, _1, _2),
        bind(Print, bind(&std::string::c_str, temp))
    )
    (foo, bar);

this one too:

    var_type<std::string>::type temp(var(std::string()));

    bind(Print,
        bind(&std::string::c_str,
            (temp = bind(Cat, _1, _2))
        )
    )
    (foo, bar);
    

Could someone comment on this?
Are these workarounds correct?
Is one better than the other?
Is there another solution?

Thanks,
-----------------------------
Éric Malenfant

A conclusion is the place where you got tired of thinking


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net