temporary-objects

Binding temporaries to non-const references in case of exceptions

Deadly 提交于 2020-01-15 08:39:05
问题 I have always read that temporaries are allowed to bind only with non-const reference arguments in case of function calls.. CASE 1:- For example:- class Simple{ public: int i; Simple(Simple &f) { i = f.i + 1; } Simple(int j) { i = j; } }; int main() { Simple f1 = Simple(2); // error no matching call fruit::fruit(fruit)... return 0; } This would give me error as I am trying to bind temporary with non-const reference arguments. CASE 2:- try { throw e; } catch ( exception& e ) { } I have learnt

Destruction order involving temporaries in Rust

雨燕双飞 提交于 2020-01-03 07:20:21
问题 In C++ (please correct me if wrong), a temporary bound via constant reference is supposed to outlive the expression it is bound to. I assumed the same was true in Rust, but I get two different behaviors in two different cases. Consider: struct A; impl Drop for A { fn drop(&mut self) { println!("Drop A.") } } struct B(*const A); impl Drop for B { fn drop(&mut self) { println!("Drop B.") } } fn main() { let _ = B(&A as *const A); // B is destroyed after this expression itself. } The output is:

Copy constructor not called when initializing an object with return value of a function

北城以北 提交于 2019-12-31 22:23:41
问题 Consider the following code: #include <iostream> using namespace std; class A { public: int a; A(): a(5) { cout << "Constructor\n"; } A(const A &b) { a = b.a; cout << "Copy Constructor\n"; } A fun(A a) { return a; } }; int main() { A a, c; A b = a.fun(c); return 0; } The output of the above code with g++ file.cpp is: Constructor Constructor Copy Constructor Copy Constructor The output of the above code with g++ -fno-elide-constructors file.cpp is: Constructor Constructor Copy Constructor Copy

const reference to temporary vs. return value optimization

与世无争的帅哥 提交于 2019-12-31 10:34:16
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

const reference to temporary vs. return value optimization

雨燕双飞 提交于 2019-12-31 10:34:12
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

How to implement thread-safe container with natural looking syntax?

痴心易碎 提交于 2019-12-23 08:34:05
问题 Preface Below code results in undefined behaviour, if used as is: vector<int> vi; ... vi.push_back(1); // thread-1 ... vi.pop(); // thread-2 Traditional approach is to fix it with std::mutex : std::lock_guard<std::mutex> lock(some_mutex_specifically_for_vi); vi.push_back(1); However, as the code grows, such things start looking cumbersome, as everytime there will be a lock before a method. Moreover, for every object, we may have to maintain a mutex. Objective Without compromising in the

Destructor call in a comma-separated expression

早过忘川 提交于 2019-12-21 10:16:34
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Destructor call in a comma-separated expression

十年热恋 提交于 2019-12-21 10:16:18
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Temporary lifetime extension

≡放荡痞女 提交于 2019-12-21 03:55:06
问题 The 12.2.5 section of standard says: A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full

Why is returning a reference to a string literal a reference to a temporary?

血红的双手。 提交于 2019-12-21 03:52:30
问题 A regular string string literal has the following definition: Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7). I'm assuming because it has static storage duration and that they're typically placed in ROM, it really isn't a big deal if there's a dangling reference to it. The following code emits a