temporary

Lifetime of rvalue bound to static const reference

放肆的年华 提交于 2019-12-04 23:39:52
Consider this: std::string foo(); void bar() { const std::string& r1 = foo(); static const std::string& r2 = foo(); } I know that the lifetime of the string resulting from the first call to foo() will be extended to the lifetime of r1 . What about the temporary bound to r2 , though? Will it live until the end of the scope or will it still be there when bar() is re-entered? Note: I am not interested whether a particular compiler does so. (I am interested in the one we use, and I can test easily with that.) I want to know what the standard has to say on this. The temporary is extended to the

Does “T const&t = C().a;” lengthen the lifetime of “a”?

风流意气都作罢 提交于 2019-12-04 22:30:13
The following scenario is given, to be interpreted as C++0x code: struct B { }; struct A { B b; }; int main() { B const& b = A().b; /* is the object still alive here? */ } Clang and GCC (trunk version as of 2011/02) behave differently: Clang lengthens the lifetime. GCC moves B to a new temporary object, and then binds the reference to that new temporary. I cannot find either behavior can be derived from the words of the Standard. The expression A().b is not a temporary (see 5.2.5). Can anyone please explain the following to me? Desired behavior (the intent of the committee) The behavior as you

Temporary Modified Environment during External Process Call from Emacs

风格不统一 提交于 2019-12-04 14:55:05
Is there a convenient and functional ( with-... -like) way of temporary modifying environment variables when using shell-comand or start-process ? Thanks in advance, Per server-with-environment looks promising. server-with-environment is a Lisp macro in `server.el'. (server-with-environment ENV VARS &rest BODY) Evaluate BODY with environment variables VARS set to those in ENV. The environment variables are then restored to their previous values. VARS should be a list of strings. ENV should be in the same format as `process-environment'. process-environment is a List of overridden environment

Prevent expression templates binding to rvalue references

老子叫甜甜 提交于 2019-12-04 02:54:29
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 ) Is there any way of designing expression templates to prevent the compiler from compiling such code that may result

When an array is created by a subexpression, what happens with the temporaries therein?

大兔子大兔子 提交于 2019-12-03 22:44:11
I was reading these two paragraphs of the FDIS (12.2p{4,5}): There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, the destruction of every temporary created in a default argument is sequenced before the construction of the next array element, if any. and The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that

Advantages of an in-memory Database in SQLite [closed]

瘦欲@ 提交于 2019-12-03 17:43:47
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I read about the keyword ":memory:" from a book on SQLite today but it only says what it is, how to use and the explanations were too short. So I searched for more information here, but couldn't get SQLite specific info. What advantages does ':memory:' mode have? (When do I need

C++0x rvalue references and temporaries

瘦欲@ 提交于 2019-12-03 06:01:09
问题 (I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f ? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } My intuition says that the f(string &&) overload should be chosen, because arg needs to be converted to a temporary no matter what, and the temporary matches the rvalue reference better than the lvalue reference.

C++0x rvalue references and temporaries

北战南征 提交于 2019-12-02 20:39:37
(I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f ? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } My intuition says that the f(string &&) overload should be chosen, because arg needs to be converted to a temporary no matter what, and the temporary matches the rvalue reference better than the lvalue reference. This is not what happens in GCC and MSVC (edit: Thanks Sumant: it doesn't happen in GCC 4.3-4.5). In at

Generate the Cartesian Product of 2 vector<string>s In-Place?

删除回忆录丶 提交于 2019-12-02 11:48:33
问题 If I want to get the Cartesian Product of these two vector<string> s: vector<string> final{"a","b","c"}; vector<string> temp{"1","2"}; But I want to put the result in final , such that final would contain: a1 a2 b1 b2 c1 c2 I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final is not important. 回答1: You may try the following approach #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string>

Return Ordering Without a vector

穿精又带淫゛_ 提交于 2019-12-02 09:02:56
So I am calling a helper function, vertex_triangle , quite a bit to allow me to take in a vector<pair<T, T>> and wind them into ordered triangles, then put those in a vector in that order. I'm using this as my return object: template <Typename T> struct Triangle { Triangle(const pair<T, T>& first, const pair<T, T>& second, const pair<T, T>& third) { data[0] = first; data[1] = second; data[2] = third; } pair<T, T> data[3]; }; So my winding helper function looks like this: template<typename T> triangle<T> vertex_triangle(const size_t index, const vector<pair<T, T>>& polygon){ if (0 == index){