temporary-objects

Memory Error with std:ostringstream and -std=c++11? [duplicate]

不想你离开。 提交于 2019-11-29 16:48:46
This question already has an answer here: stringstream, string, and char* conversion confusion 5 answers EDIT : Thanks to everyone who pointed out the problem, and that it was discussed on Stack Overflow. I cast the last close vote myself. A related question: neither CPP Reference on ostringstream or ostringstream::str state its a temporary. How did so many people know? Or is there different documentation I should have consulted? I'm having a lot of trouble with memory errors under Debian 7.3 (x64) with GCC 4.7.2, -std=c++11 and std::ostringstream . Its leading to bizaare results like https:/

What is the lifetime of a default argument temporary bound to a reference parameter?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 06:20:44
I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory: #include <iostream> struct X{ ~X(){ std::cout << "Goodbye, cruel world!\n"; } }; X const& f(X const& x = X()){ std::cout << "Inside f()\n"; return x; } void g(X const& x){ std::cout << "Inside g()\n"; } int main(){ g(f()); } Live example. Output: Inside f() Inside g() Goodbye, cruel world! So it seems the temporary is destroyed after g() is called... what gives? The standard handles this in a special case in §12.2 [class.temporary] :

Is temporary object originally const?

社会主义新天地 提交于 2019-11-29 02:55:19
问题 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() ? 回答1: 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

Memory Error with std:ostringstream and -std=c++11? [duplicate]

試著忘記壹切 提交于 2019-11-28 11:57:54
问题 This question already has an answer here: stringstream, string, and char* conversion confusion 5 answers EDIT : Thanks to everyone who pointed out the problem, and that it was discussed on Stack Overflow. I cast the last close vote myself. A related question: neither CPP Reference on ostringstream or ostringstream::str state its a temporary. How did so many people know? Or is there different documentation I should have consulted? I'm having a lot of trouble with memory errors under Debian 7.3

C++ destruction of temporary object in an expression

谁说我不能喝 提交于 2019-11-27 22:37:33
Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) { std::cout << "ctor" << std::endl; } ~implicit_t() { std::cout << "dtor" << std::endl; } int x_m; }; std::ostream& operator<<(std::ostream& s, const implicit_t& x) { return s << x.x_m; } const implicit_t& f(const implicit_t& x) { return x; } int main() { std::cout << f(42) << std::endl; return 0; } I get the following output: ctor 42 dtor While I know this is correct, I'm not certain why . Is there anyone with stdc++ knowledge who can explain it to me? Temporary objects are destroyed as the last step

Where are temporary object stored?

时光怂恿深爱的人放手 提交于 2019-11-27 14:26:27
IMO temporary objects are stored in dynamic (heap) memory, but I'm not sure. Can you please confirm or deny my thoughts? The standard does not mandate any memory area (heap/stack) for them, but they are just like local variables "automatic storage", that is at the end of the expression (or longer when bound to a ref-to-const) they are destructed. Most implementations will store them on the stack just like local variables. edit: As James Kanze pointed out: In the case the lifetime of a temporary is extended via a ref-to-const, its store location is on most implementations somewhat determined by

Why can a non-const reference parameter be bound to a temporary object?

可紊 提交于 2019-11-27 03:39:33
问题 char f1(); void f2(char&); struct A {}; A f3(); void f4(A&); int main() { f2(f1()); // error C2664. This is as expected. f4(f3()); // OK! Why??? } error C2664: 'void f4(char &)' : cannot convert argument 1 from 'char' to 'char &' I have been taught that in C++ a non-const reference parameter cannot be bound to a temporary object; and in the code above, f2(f1()); triggers an error as expected. However, why does the same rule not apply to the code line f4(f3()); ? PS: My compiler is VC++ 2013.

C++ destruction of temporary object in an expression

删除回忆录丶 提交于 2019-11-26 21:04:15
问题 Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) { std::cout << "ctor" << std::endl; } ~implicit_t() { std::cout << "dtor" << std::endl; } int x_m; }; std::ostream& operator<<(std::ostream& s, const implicit_t& x) { return s << x.x_m; } const implicit_t& f(const implicit_t& x) { return x; } int main() { std::cout << f(42) << std::endl; return 0; } I get the following output: ctor 42 dtor While I know this is correct, I'm not certain why . Is there

Where are temporary object stored?

会有一股神秘感。 提交于 2019-11-26 17:47:08
问题 IMO temporary objects are stored in dynamic (heap) memory, but I'm not sure. Can you please confirm or deny my thoughts? 回答1: The standard does not mandate any memory area (heap/stack) for them, but they are just like local variables "automatic storage", that is at the end of the expression (or longer when bound to a ref-to-const) they are destructed. Most implementations will store them on the stack just like local variables. edit: As James Kanze pointed out: In the case the lifetime of a

Temporary objects - when are they created, how do you recognise them in code?

ⅰ亾dé卋堺 提交于 2019-11-26 08:47:27
问题 In Eckel, Vol 1, pg:367 //: C08:ConstReturnValues.cpp // Constant return by value // Result cannot be used as an lvalue class X { int i; public: X(int ii = 0); void modify(); }; X::X(int ii) { i = ii; } void X::modify() { i++; } X f5() { return X(); } const X f6() { return X(); } void f7(X& x) { // Pass by non-const reference x.modify(); } int main() { f5() = X(1); // OK -- non-const return value f5().modify(); // OK // Causes compile-time errors: //! f7(f5()); //! f6() = X(1); //! f6()