[lambda] Nesting and temporaries
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_typestd::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_typestd::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
participants (1)
-
EMalenfant@interstarinc.com