temporary

How to pass a temporary array?

…衆ロ難τιáo~ 提交于 2019-11-28 08:01:59
问题 How can I pass a temporary array? I want to do something like this: #include <iostream> int sum(int arr[]) { int answer = 0; for (const auto& i : arr) { answer += i; } return answer; } int main() { std::cout << sum( {4, 2} ) << std::endl; // error std::cout << sum( int[]{4, 2} ) << std::endl; // error } Do I need a positive integer literal in the function parameter's braces [] ? If I include that literal, will it limit what arrays I can pass to only arrays of that size? Also, how can I pass

rvalues and temporary objects in the FCD

北城以北 提交于 2019-11-28 04:09:36
问题 It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75: An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. I can't believe my eyes. This must be an error, right? To clarify, here is how I understand the terms: #include <string> void foo(std::string&& str) { std::cout << str << std::endl; } int main() { foo(std::string("hello")); }

MySQL: Auto increment temporary column in select statement

ぃ、小莉子 提交于 2019-11-28 03:56:30
How do I create and auto increment a temporary column in my select statement with MySQL? Here is what I have so far: SET @cnt = 0; SELECT (@cnt =@cnt + 1) AS rowNumber, rowID FROM myTable WHERE CategoryID = 1 Which returns: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + (NULL) | 1 + + (NULL) | 25 + + (NULL) | 33 + + (NULL) | 150 + + (NULL) | 219 + +++++++++++++++++++++ But I need: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + 1 | 1 + + 2 | 25 + + 3 | 33 + + 4 | 150 + + ... | ... + +++++++++++++++++++++ Kermit This will give you a consecutive row

Const reference to temporary

本秂侑毒 提交于 2019-11-27 18:06:15
问题 After reading this article on Herb Sutter's blog, I experimented a bit and ran into something that puzzles me. I am using Visual C++ 2005, but I would be surprised if this was implementation dependent. Here is my code: #include <iostream> using namespace std; struct Base { //Base() {} ~Base() { cout << "~Base()" << endl; } }; int main() { const Base & f = Base(); } When run, it displays " ~Base() " twice ... But if I un-comment the constructor, it displays it only once ! Does anyone have an

C++: non-temporary const reference

六月ゝ 毕业季﹏ 提交于 2019-11-27 16:17:44
问题 I need to write a class whose constructor takes a constant reference to a object and stores it locally. In order to avoid most common mistakes I can foresee, I'd like to only accept references to non-temporary (ie: references to lvalues). How can I write a function that takes constant references to non-temporary only? Of course even a non-temporary could go out of scope and thus break my class behavior, but I believe that by disallowing temporary references I will avoid most mistakes. 回答1: If

Turning temporary stringstream to c_str() in single statement

大城市里の小女人 提交于 2019-11-27 16:01:36
问题 Consider the following function: void f(const char* str); Suppose I want to generate a string using stringstream and pass it to this function. If I want to do it in one statement, I might try: f((std::ostringstream() << "Value: " << 5).str().c_str()); // error This gives an error: 'str()' is not a member of 'basic_ostream'. OK, so operator<< is returning ostream instead of ostringstream - how about casting it back to an ostringstream? 1) Is this cast safe? f(static_cast<std::ostringstream&>

Why do I need std::get_temporary_buffer?

徘徊边缘 提交于 2019-11-27 09:43:05
问题 For what purpose I should use std::get_temporary_buffer? Standard says the following: Obtains a pointer to storage sufficient to store up to n adjacent T objects. I thought that the buffer will be allocated on the stack, but that is not true. According to the C++ Standard this buffer is actually not temporary. What advantages does this function have over the global function ::operator new , which doesn't construct the objects either. Am I right that the following statements are equivalent?

Why does writing to temporary stream fail?

烂漫一生 提交于 2019-11-27 06:54:19
问题 Consider the following code: #include <sstream> #include <iostream> class Foo : public std::stringstream { public: ~Foo() { std::cout << str(); } }; int main() { Foo foo; foo << "Test1" << std::endl; Foo() << "Test2" << std::endl; return 0; } When I execute this, it gives me: 004177FC Test1 I do not understand why the second example gives me gibberish. The temporary should live until the entire expression is evaluated, so why does it not behave the same as the first example? 回答1: I tested it.

MySQL: Auto increment temporary column in select statement

落爺英雄遲暮 提交于 2019-11-27 05:11:38
问题 How do I create and auto increment a temporary column in my select statement with MySQL? Here is what I have so far: SET @cnt = 0; SELECT (@cnt =@cnt + 1) AS rowNumber, rowID FROM myTable WHERE CategoryID = 1 Which returns: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + (NULL) | 1 + + (NULL) | 25 + + (NULL) | 33 + + (NULL) | 150 + + (NULL) | 219 + +++++++++++++++++++++ But I need: +++++++++++++++++++++ + rowNumber | rowID + +++++++++++++++++++++ + 1 | 1 + + 2 | 25 + + 3 |

Why lifetime of temporary doesn't extend till lifetime of enclosing object?

痞子三分冷 提交于 2019-11-27 04:29:19
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 second object? I tried this and it didn't work. I don't exactly understand this. I wrote this code: struct