temporary

Using index, using temporary, using filesort - how to fix this?

空扰寡人 提交于 2019-11-27 03:49:00
I'm working on a event tracking system which uses a handful of lookup tables as well as the primary logging table. In a report I'm writing, an object can be selected to view statistics against. The interface shows all objects in order of decreasing importance (ie, hits). The schema for the two tables (slightly trimmed down, but you get the gist): CREATE TABLE IF NOT EXISTS `event_log` ( `event_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(5) DEFAULT NULL, `object_id` int(5) DEFAULT NULL, `event_date` datetime DEFAULT NULL, PRIMARY KEY (`event_id`), KEY `user_id` (`user_id`), KEY `object

Why is taking the address of a temporary illegal?

半城伤御伤魂 提交于 2019-11-27 02:46:06
问题 I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my question is WHY? Let us consider the following code class empty{}; int main() { empty x = empty(); //most compilers would elide the temporary return 0; } The accepted answer here mentions "usually the compiler consider the temporary and the copy constructed as

constant references with typedef and templates in c++

好久不见. 提交于 2019-11-26 22:52:57
I heard the temporary objects can only be assigned to constant references. But this code gives error #include <iostream.h> template<class t> t const& check(){ return t(); //return a temporary object } int main(int argc, char** argv){ const int &resCheck = check<int>(); /* fine */ typedef int& ref; const ref error = check<int>(); / *error */ return 0; } The error that is get is invalid initialization of reference of type 'int&' from expression of type 'const int' This: typedef int& ref; const ref error; Doesn't do what you think it does. Consider instead: typedef int* pointer; typedef const

About binding a const reference to a sub-object of a temporary

那年仲夏 提交于 2019-11-26 22:13:20
问题 With code like #include <iostream> struct P { int x; P(int x) : x(x) {} ~P() { std::cout << "~P()\n"; } }; int main() { auto const& x = P{10}.x; std::cout << "extract\n"; } GCC prints ~P() extract , indicating that the temporary's lifetime is not extended by the reference. By contrast, Clang (IMO correctly) extends the lifetime of the temporary to the lifetime of the reference x and the destructor will be therefore called after the output in main . Note that GCC suddenly shows Clang's

Disallowing creation of the temporary objects

我的梦境 提交于 2019-11-26 20:53:51
While debugging crash in a multithreaded application I finally located the problem in this statement: CSingleLock(&m_criticalSection, TRUE); Notice that it is creating an unnamed object of CSingleLock class and hence the critical section object gets unlocked immediately after this statement. This is obviously not what the coder wanted. This error was caused by a simple typing mistake. My question is, is there someway I can prevent the temporary object of a class being created at the compile time itself i.e. the above type of code should generate a compiler error. In general, I think whenever a

prolonging the lifetime of temporaries

假装没事ソ 提交于 2019-11-26 18:16:39
问题 What is the design rationale behind allowing this const Foo& a = function_returning_Foo_by_value(); but not this Foo& a = function_returning_Foo_by_value(); ? What could possible go wrong in the second line (which would not already go wrong in the first line)? 回答1: I'll answer your question... the other way around. Why did they allowed Foo const& foo = fooByValue(); to begin with ? It makes life (somewhat) easier, but introduces potential undefined behavior all over the place. Foo const&

Why is it legal to borrow a temporary?

做~自己de王妃 提交于 2019-11-26 12:33:47
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? 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 temporary live in Rust? And since x is only a borrow, who is the owner of the string? The reference says :

Using index, using temporary, using filesort - how to fix this?

强颜欢笑 提交于 2019-11-26 10:34:35
问题 I\'m working on a event tracking system which uses a handful of lookup tables as well as the primary logging table. In a report I\'m writing, an object can be selected to view statistics against. The interface shows all objects in order of decreasing importance (ie, hits). The schema for the two tables (slightly trimmed down, but you get the gist): CREATE TABLE IF NOT EXISTS `event_log` ( `event_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(5) DEFAULT NULL, `object_id` int(5) DEFAULT

Why lifetime of temporary doesn&#39;t extend till lifetime of enclosing object?

我的未来我决定 提交于 2019-11-26 09:45:22
问题 I know that a temporary cannot be bound to a non-const reference, but it can be bound to const reference. That is, A & x = A(); //error const A & y = A(); //ok I also know that in the second case (above), the lifetime of the temporary created out of A() extends till the lifetime of const reference (i.e y ). But my question is: Can the const reference which is bound to a temporary, be further bound to yet another const reference, extending the lifetime of the temporary till the lifetime of

Do temp variables slow down my program?

落爺英雄遲暮 提交于 2019-11-26 08:13:50
问题 Suppose I have the following C code: int i = 5; int j = 10; int result = i + j; If I\'m looping over this many times, would it be faster to use int result = 5 + 10 ? I often create temporary variables to make my code more readable, for example, if the two variables were obtained from some array using some long expression to calculate the indices. Is this bad performance-wise in C? What about other languages? 回答1: A modern optimizing compiler should optimize those variables away, for example