temporary-objects

Does const reference prolong the life of a temporary object returned by a temporary object?

戏子无情 提交于 2019-12-02 09:02:20
I know that const reference prolongs the life of a temporary locally. Now I am asking myself if this propriety can be extended on a chain of temporary objects, that is, if I can safely define: std::string const& foo = aBar.getTemporaryObject1().getTemporaryObject2(); My feeling is that, since the the first method aBar.getTemporaryObject1() returns already a temporary object, the propriety doesn't hold for aBar.getTemporaryObject2() . The lifetime extension only applies when a reference is directly bound to that temporary. For example, initializing another reference from that reference does not

Usage of string::c_str on temporary string [duplicate]

我们两清 提交于 2019-12-01 04:00:29
This question already has an answer here: C++ destruction of temporary object in an expression 4 answers In regards to when temporary objects get destroyed, is this valid: FILE *f = fopen (std::string ("my_path").c_str (), "r"); Will the temporary be destroyed immediately after having evaluated the first argument to fopen or after the fopen call. Testing with the following code: #include <cstdio> using namespace std; struct A { ~A() { printf ("~A\n"); } const char *c_str () { return "c_str"; } }; void foo (const char *s) { printf ("%s\n", s); } int main () { foo (A().c_str()); printf ("after\n

Performance of pIter != cont.end() in for loop

蹲街弑〆低调 提交于 2019-12-01 02:46:58
I was getting through "Exceptional C++" by Herb Sutter lately, and I have serious doubts about a particular recommendation he gives in Item 6 - Temporary Objects. He offers to find unnecessary temporary objects in the following code: string FindAddr(list<Employee> emps, string name) { for (list<Employee>::iterator i = emps.begin(); i != emps.end(); i++) { if( *i == name ) { return i->addr; } } return ""; } As one of the example, he recommends to precompute the value of emps.end() before the loop, since there is a temporary object created on every iteration: For most containers (including list)

Usage of string::c_str on temporary string [duplicate]

Deadly 提交于 2019-12-01 01:54:54
问题 This question already has answers here : C++ destruction of temporary object in an expression (4 answers) Closed 5 years ago . In regards to when temporary objects get destroyed, is this valid: FILE *f = fopen (std::string ("my_path").c_str (), "r"); Will the temporary be destroyed immediately after having evaluated the first argument to fopen or after the fopen call. Testing with the following code: #include <cstdio> using namespace std; struct A { ~A() { printf ("~A\n"); } const char *c_str

When exactly is an initializer temporary destroyed?

会有一股神秘感。 提交于 2019-11-30 13:44:04
问题 I constructed this experiment today, after answering some question struct A { bool &b; A(bool &b):b(b) { } ~A() { std::cout << b; } bool yield() { return true; } }; bool b = A(b).yield(); int main() { } b has value false (resulting from zero initialization) before setting it to true by the dynamic initialization. If the temporary is destroyed before initialization of b finished, we will print false , otherwise true . The spec says that the temporary is destroyed at the end of the full

Lifetime extension and the conditional operator

隐身守侯 提交于 2019-11-30 13:42:33
问题 local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d :

Lifetime extension and the conditional operator

情到浓时终转凉″ 提交于 2019-11-30 08:09:14
local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d : std::string("world"); Does C++ mandate the lifetime of the temporary be extended when the condition is

Undefined behavior and temporaries

时光怂恿深爱的人放手 提交于 2019-11-30 06:46:23
1) Is it undefined behavior to return a reference to a temporary, even if that reference is not used? For example, is this program guaranteed to output "good": int& func() { int i = 5; return i; } int main() { func(); cout << "good" << endl; return 0; } 2) Is it undefined behavior to simply have a reference to an object that no longer exists, even if that reference is not used? For example, is this program guaranteed to output "good": int main() { int *j = new int(); int &k = *j; delete j; cout << "good" << endl; return 0; } 3) Is it undefined behavior to combine these? int& func() { int i = 5

Is temporary object originally const?

拈花ヽ惹草 提交于 2019-11-30 05:06:12
Is this code UB? struct A { void nonconst() {} }; const A& a = A{}; const_cast<A&>(a).nonconst(); In other words, is the (temporary) object originally const ? I've looked through the standard but cannot find an answer so would appreciate quotations to relevant sections. Edit: for those saying A{} is not const , then can you do A{}.nonconst() ? The initialization of the reference a is given by [dcl.init.ref]/5 (bold mine): Otherwise, if the initializer expression is an rvalue (but not a bit-field)[...] then the value of the initializer expression in the first case and the result of the

Why doesn't a const reference extend the life of a temporary object passed via a function?

时光怂恿深爱的人放手 提交于 2019-11-30 04:38:58
In the following simple example, why can't ref2 be bound to the result of min(x,y+1) ? #include <cstdio> template< typename T > const T& min(const T& a, const T& b){ return a < b ? a : b ; } int main(){ int x = 10, y = 2; const int& ref = min(x,y); //OK const int& ref2 = min(x,y+1); //NOT OK, WHY? return ref2; // Compiles to return 0 } live example - produces: main: xor eax, eax ret EDIT: Below example better described a situation, I think. #include <stdio.h> template< typename T > constexpr T const& min( T const& a, T const& b ) { return a < b ? a : b ; } constexpr int x = 10; constexpr int y