temporary

C++: constant reference to temporary

落爺英雄遲暮 提交于 2019-12-12 21:42:38
问题 There are several questions about lifetime of constant reference on SO, but still I don't get it. Is this piece of code valid? struct S { const int &ref; S( const int &x ) : ref(x) { } }; int main( ) { S s( 0 ); // ... use( s.ref ); // ... return 0; } Intuitively I'd say no, since 0 should expire after the expression ( S s(0); ) is evaluated. However both GCC and CLANG compile it fine, without warnings, and valgrind doesn't detect any runtime error. What am I missing about references? 回答1:

Temporary and expression behavior

a 夏天 提交于 2019-12-12 17:06:40
问题 Is this well defined behavior? const char* p = (std::string("Hello") + std::string("World")).c_str(); std::cout << p; I am not sure. Reasons? 回答1: No, this is undefined behavior. Both std::string temporaries and the temporary returned by operator+ only live until the end of the initialization of your const char* (end of full expression). Then they are destroyed and p points to uncertain memory. 回答2: No the behaviour is undefined because p points to deallocated storage in std::cout << p; A

How to realize complex search filters in couchdb? Should I avoid temporary views?

丶灬走出姿态 提交于 2019-12-12 11:52:05
问题 I want to administrate my User-entities in a grid. I want to sort them and I want to have a search filter for each column. My dynamic generated temporary view works fine: function(doc){ if(doc.type === 'User' && // Dynamic filters: WHERE firstName LIKE '%jim%' AND lastName LIKE '%knopf%' (doc.firstName.match(/.*?jim.*?/i) && doc.lastName.match(/.*?knopf.*?/i)) ) { // Dynamic sort emit(doc.lastName, doc); } } BUT everywhere is written you have to AVOID temporary views. IS there a better way?

Returning temporaries of type with deleted move/copy ctor

瘦欲@ 提交于 2019-12-12 10:47:46
问题 Consider the following program: #include<iostream> using namespace std; struct S { S() = default; S(const S& other) = delete; S(S&& other) = delete; int i; }; S nakedBrace() { return {}; // no S constructed here? } S typedBrace() { return S{}; } int main() { // produce an observable effect. cout << nakedBrace().i << endl; // ok cout << typedBrace().i << endl; // error: deleted move ctor } Sample session: $ g++ -Wall -std=c++14 -o no-copy-ctor no-copy-ctor.cpp no-copy-ctor.cpp: In function 'S

Does a const reference class member prolong the life of a temporary?

断了今生、忘了曾经 提交于 2019-12-12 01:57:06
问题 Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout << "The answer is: " << sandbox.member << endl; return 0; } Give output of: The answer is: Instead of: The answer is: four 回答1: Only local const references prolong the lifespan. The standard specifies such behavior in §8.5.3/5, [dcl.init.ref], the section on initializers of

Why is it legal to borrow a temporary?

落爺英雄遲暮 提交于 2019-12-11 08:37:34
问题 Coming from C++, I'm rather surprised that this code is valid in Rust: let x = &mut String::new(); x.push_str("Hello!"); In C++, you can't take the address of a temporary, and a temporary won't outlive the expression it appears in. How long does the temporary live in Rust? And since x is only a borrow, who is the owner of the string? 回答1: Why is it legal to borrow a temporary? It's legal for the same reason it's illegal in C++ — because someone said that's how it should be. How long does the

Is it necessary to have a temporary or a literal to have an rvalue?

自古美人都是妖i 提交于 2019-12-11 08:13:30
问题 This question asks if all temporaries are rvalue. The answer is no, because if we consider this expression: const int &ri = 2 + 3; then, the very same temporary (2 + 3) , which is an rvalue here, can be used as an lvalue in a subsequent expression: const int *pi = &ri; so this temporary is not (only) an rvalue. The logic statement temporary ==> rvalue is then false. However, we cannot write const int &ri = &(2 + 3); // illegal, 2 + 3 -> temporary -> rvalue or int *i = &4; // illegal, 4 is an

C++ temporary string lifetime

心不动则不痛 提交于 2019-12-10 15:53:46
问题 Apologies as I know similar-looking questions exist, but I'm still not totally clear. Is the following safe? void copyStr(const char* s) { strcpy(otherVar, s); } std::string getStr() { return "foo"; } main() { copyStr(getStr().c_str()); } A temporary std::string will store the return from getStr(), but will it live long enough for me to copy its C-string elsewhere? Or must I explicitly keep a variable for it, eg std::string temp = getStr(); copyStr(temp.c_str()); 回答1: Yes, it's safe. The

rules with temporary objects and args by reference

我是研究僧i 提交于 2019-12-10 10:30:38
问题 say I have a class: class A { public: A() {} }; and a function: void x(const A & s) {} and I do: x(A()); could someone please explain to me the rules regarding passing temporary objects by reference? In terms of what the compiler allows, where you need const, if an implicit copy happens, etc. From playing around, it seems like you need the const which makes sense, but is there a formal rule regarding all this? Thanks! 回答1: There is a formal rule - the C++ Standard (section 13.3.3.1.4 if you

Prevent expression templates binding to rvalue references

你离开我真会死。 提交于 2019-12-09 15:39:31
问题 I understand that doing something like the following: auto&& x = Matrix1() + Matrix2() + Matrix3(); std::cout << x(2,3) << std::endl; Will cause a silent runtime error if the matrix operations use expression templates (such as boost::ublas). Is there any way of designing expression templates to prevent the compiler from compiling such code that may result in the use of expired temporaries at runtime? (I've attempted unsuccessfully to work around this issue, the attempt is here) 回答1: Is there