temporary-objects

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

ぃ、小莉子 提交于 2019-12-19 05:04:35
问题 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

Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 12:55:40
问题 I would like to check my understanding and conclusions on this matter. On IRC, it was asked: Is it acceptable to const_cast a const reference that's bound to a temporary object? Translating: he has a ref-to-const bound to a temporary, and he wants to cast away its const -ness to modify it. My response was that I'd asked a similar question previously, where the consensus seemed to be that temporaries themselves are not inherently const , and thus that you can cast off the const -ness of a

Undefined behavior and temporaries

会有一股神秘感。 提交于 2019-12-18 12:24:55
问题 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;

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

时光怂恿深爱的人放手 提交于 2019-12-18 04:02:38
问题 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

const reference to a temporary object becomes broken after function scope (life time)

南楼画角 提交于 2019-12-17 12:46:27
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . While asking this question, I learned const reference to a temporary object is valid in C++: int main () { int a = 21; int b = 21; //error: invalid initialization of non-const reference //int & sum = a + b;e [...] //OK int const & sum = a + b; return sum; } But in the following example, the const reference refnop refers to a destroyed temporary object. I

C++ brace initializer list, temporary lifetime

萝らか妹 提交于 2019-12-13 03:44:30
问题 I've got following code: string join(initializer_list<string_view> strings); initializer_list is std::initializer_list and string_view isn't std::string view but very similar class with constructors from const string& and const char*. Then I've got following invocation of join : EXPECT_EQ("this", join({ string("this") })); After small investigation I've found that the first element of resulting initializer list isn't "this" but "\0his" . This is becouse the destructor of the temporary created

Passing a pointer to temporary object

丶灬走出姿态 提交于 2019-12-12 04:58:22
问题 We know that we can pass temporary objects to functions by const reference, like this: class A { public: A(int _b = 0) { b = _b; } int b; }; void foo(A& a) {printf("%d", a.b);} void cfoo(const A& a) {printf("%d", a.b);} int main(void) { //foo(A(4)); doesn't compile cfoo(A(5)); } but what about passing by pointer? why does this compile? void pfoo(A* pa) {pa->b = 19;} int main(void) { pfoo(&A(5)); } 回答1: but what about passing anonymous variables pointer? why does this compile? You are probably

Why not non-const reference to temporary objects? [duplicate]

眉间皱痕 提交于 2019-12-12 04:32:25
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Does a const reference prolong the life of a temporary? prolonging the lifetime of temporaries C++ allows assignment of temporary objects only to const reference. It wont allow assignement of temporary objects to reference. For example: String& a = String("test"); // Error const String& a = String("test"); // Ok Everywhere I google for this result, i only see the following answers Modifying temporary objects

What constitutes of RValues?

不问归期 提交于 2019-12-12 03:04:02
问题 RValues are things which are not maniputable regions of memory, so literals like integers are considered RValues. Do constants constitute RValues? const int x = 0; is maniputable at least one time. Now, the temporary objects created by the compiler are also RValues even when they have maniputable memory regions. Why is that so? Because they cannot be modified by "users"? Is this the reason? So, a memory region which is NOT maniputable by the "users" is called RValue? 回答1: Scalar rvalues are

Move from temporary used in method chaining

孤街浪徒 提交于 2019-12-11 08:37:14
问题 I am trying to do something similar to this: #include <vector> #include <memory> struct Bar { Bar& doThings() {return *this;} std::unique_ptr<int> m_content; // A non-copyable type }; struct Foo { Foo& append(Bar&& obj) { objects.push_back(std::move(obj)); return *this; } std::vector<Bar> objects; }; int test() { Foo test; test.append(std::move(Bar{}.doThings())) //Ok // Not ok .append(Bar{}.doThings()) ; } error: cannot bind rvalue reference of type Bar&& to lvalue of type Bar Is it possible