temporary-objects

Prolonging life of a temporary object using const reference

流过昼夜 提交于 2019-12-11 06:15:35
问题 I need a few clarification regarding const reference. From this link: const Foo &myFoo = FuncBar(); const reference extended the life of the local object. But when I checked this link although they used a const reference Sandbox(const string& n) : member(n) {} the lifetime of string "four" did not increase. Sandbox sandbox(string("four")); They used the sentence Only local const references prolong the lifespan. Then in the second link isn't the string "four" local to the main function and

Lambda capturing rvalue reference by reference

我的梦境 提交于 2019-12-10 22:09:34
问题 Is below code standard-correct? (godbolt) I.e. by-ref capturing a forwarding reference that represents a temporary, and returning the resulting lambda by-value from the function, within the same expression . Of course storing the lambda for later use would make it contain a dangling reference, but I'm referring to the exact usage inside main . The doubts I'm having relate to this SO answer and potentially this language defect. Specifically there is one daunting comment that says "the

static_cast and temporary creation (final edition)

匆匆过客 提交于 2019-12-10 08:48:57
问题 Prerequisities: To understand this question, please, read the following question and its answer at first: Cast auto_ptr<Base> to auto_ptr<Derived> At Cast auto_ptr<Base> to auto_ptr<Derived> Steve answered that "Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the statement)." I'm interested in the process of temporary creation while static_cast is called. I would like to have the code

Visual Studio is not creating temporary object when typecasting?

吃可爱长大的小学妹 提交于 2019-12-09 11:05:10
问题 I'm using Visual Studio Express 2013 and is fooling around a bit trying to learn about different things in C++. I stumbled upon an interesting bug in the compiler where it doesn't seem to create a temporary object when explicitly type casting to the same type as the reference. #include <iostream> using namespace std; int main() { int number; // float number; number = 2; const int& plainref_i = number; const int& recastref_i = (int)number; // this goes wrong if number is int const float&

Cv-qualifications of prvalues (revisited)

旧城冷巷雨未停 提交于 2019-12-09 05:06:07
问题 This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intended to solve some inconsistencies (e.g. functions returning prvalues and declared with cv-qualified return types). However, I see another place in the standard that appears to rely on prvalues having cv-qualified types: initialization of const references with prvalues through temporary materialization

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

半城伤御伤魂 提交于 2019-12-08 23:05:09
问题 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

sqlite: temporary table/view in a read-only db?

坚强是说给别人听的谎言 提交于 2019-12-07 06:49:25
问题 It seems that sqlite won't allow me to create a temporary view in a read-only db. Am I missing something? If it's TEMPORARY, I figured db connection mode shouldn't matter. I even specified "PRAGMA temp_store = MEMORY" -- it didn't help. Is there a reasonable alternative to using views? 回答1: You can create a temporary view that references data in the main db. CREATE VIEW temp.userview AS select userid, firstname, lastname from main.usertbl; Then access the view like such... SELECT * from temp

const reference to temporary reference

心已入冬 提交于 2019-12-07 01:33:51
问题 #include <iostream> using namespace std; struct CL { CL() { cout<<"CL()"<<endl; } CL(const CL&) { cout<<"CL(const CL&)"<<endl; } ~CL() { cout<<"~CL()"<<endl; } }; CL cl; CL fnc() { return cl; } int main() { cout<<"start"<<endl; const CL& ref=static_cast<const CL&>(fnc()); //...Is "ref" valid here?? cout<<"end"<<endl; return 0; } What's lifetime of temporary object returned by fnc()? Is it lifetime of "ref" or of temporary reference static_cast(fnc()), which destroyed at end of statement?

prohibiting instantiation as a temporary object (C++)

六月ゝ 毕业季﹏ 提交于 2019-12-06 20:17:06
问题 I like using sentry classes in c++, but I seem to have a mental affliction that results in repeatedly writing bugs like the following: { MySentryClass(arg); // ... other code } Needless to say, this fails because the sentry dies immediately after creation, rather than at the end of the scope, as intended. Is there some way to prevent MySentryClass from being instantiated as a temporary, so that the above code either fails to compile, or at least aborts with an error message at runtime? 回答1: I

Is there any C++ compiler which can issue a warning for a dangling reference?

吃可爱长大的小学妹 提交于 2019-12-06 04:39:21
问题 Given the following code, where x is a dangling const reference to a vanished object, and is therefore undefined behavior. auto get_vec() { return std::vector<int>{1,2,3,4,5}; } const auto& x = get_vec().back(); It seems like neither GCC 7.3 , Clang 6.0 and MSVC is able to emit a warning, even with all warnings enabled. Does anyone know if it is any way to emit a warning in these cases? Is there any difference between const auto& and auto&& in these cases? Note, if back() would return by