Friday, 15 May 2015

C++: when object constructed in argument is destructed? -



C++: when object constructed in argument is destructed? -

when object constructed in argument destructed, before of after function call?

e.g. next code safe?

void f(const char*) { ... } std::string g() { ... } ... f(g().c_str());

it works me don't know undefined behaviour or should work.

g() temporary. lifetime of tempraries extends entire time of evaluation of entire full-expression (in case, f(g().c_str())) hence usage safe, unless f() stores pointer somewhere.

§12.2/4 there 2 contexts in temporaries destroyed @ different point end of fullexpression. first context when look appears initializer declarator defining object. in context, temporary holds result of look shall persist until object’s initialization complete. [...]

§12.2/5 sec context when reference bound temporary. [...]

neither of these 2 cases apply in example.

c++

No comments:

Post a Comment