temporary

stringstream temporary ostream return problem

倖福魔咒の 提交于 2019-11-29 09:39:45
问题 I'm creating a logger with the following sections: // #define LOG(x) // for release mode #define LOG(x) log(x) log(const string& str); log(const ostream& str); With the idea to do: LOG("Test"); LOG(string("Testing") + " 123"); stringstream s; LOG(s << "Testing" << 1 << "two" << 3); This all works as intended, but when I do: LOG(stringstream() << "Testing" << 1 << "two" << 3); It does not work: void log(const ostream& os) { std::streambuf* buf = os.rdbuf(); if( buf && typeid(*buf) == typeid

Aggregate reference member and temporary lifetime

与世无争的帅哥 提交于 2019-11-29 07:28:28
问题 Given this code sample, what are the rules regarding the lifetime of the temporary string being passed to S . struct S { // [1] S(const std::string& str) : str_{str} {} // [2] S(S&& other) : str_{std::move(other).str} {} const std::string& str_; }; S a{"foo"}; // direct-initialization auto b = S{"bar"}; // copy-initialization with rvalue std::string foobar{"foobar"}; auto c = S{foobar}; // copy-initialization with lvalue const std::string& baz = "baz"; auto d = S{baz}; // copy-initialization

Full-expression boundaries and lifetime of temporaries [duplicate]

拈花ヽ惹草 提交于 2019-11-29 06:28:52
Possible Duplicate: C++: Life span of temporary arguments? It is said that temporary variables are destroyed as the last step in evaluating the full-expression, e.g. bar( foo().c_str() ); temporary pointer lives until bar returns, but what for the baz( bar( foo().c_str() ) ); is it still lives until bar returns, or baz return means full-expression end here, compilers I checked destruct objects after baz returns, but can I rely on that? Temporaries life until the end of the full expression in which they are created. A "full expression" is an expression that's not a sub-expression of another

Const reference to temporary

狂风中的少年 提交于 2019-11-29 04:09:23
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 explanation for this? This IS implementation dependent. The standard allows a copy to occur when binding

C++ function returns a rvalue, but that can be assigned a new value?

拟墨画扇 提交于 2019-11-29 02:13:40
The code is as follows: #include <iostream> using namespace std; class A { }; A rtByValue() { return A(); } void passByRef(A &aRef) { // do nothing } int main() { A aa; rtByValue() = aa; // compile without errors passByRef(rtByValue()); // compile with error return 0; } The g++ compiler gives the following error: d.cpp: In function ‘int main()’: d.cpp:19:23: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’ d.cpp:12:6: error: in passing argument 1 of ‘void passByRef(A&)’ It says that I can't pass an rvalue as an argument of a non-const reference, but

C++: non-temporary const reference

你说的曾经没有我的故事 提交于 2019-11-29 01:53:06
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. If you are going to store a reference and need to use it after the constructor has completed, it's probably

Returning a c++ std::vector without a copy?

吃可爱长大的小学妹 提交于 2019-11-29 01:41:14
问题 Is it possible to return a standard container from a function without making a copy? Example code: std::vector<A> MyFunc(); ... std::vector<A> b = MyFunc(); As far as I understand, this copies the return value into a new vector b. Does making the function return references or something like that allow avoiding the copy? 回答1: If your compiler supports the NRVO then no copy will be made, provided certain conditions are met in the function returning the object. Thankfully, this was finally added

Why does writing to temporary stream fail?

不想你离开。 提交于 2019-11-28 12:18:06
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? I tested it. I can guess that operator<< cannot bind a temporary to a non-const reference, so any externally defined

How do I make format! return a &str from a conditional expression?

送分小仙女□ 提交于 2019-11-28 10:11:22
问题 I happened upon this problem where format! creates a temporary value in a pattern that is not anchored to anything, as far as I understand it. let x = 42; let category = match x { 0...9 => "Between 0 and 9", number @ 10 => format!("It's a {}!", number).as_str(), _ if x < 0 => "Negative", _ => "Something else", }; println!("{}", category); In this code, the type of category is a &str , which is satisfied by returning a literal like "Between 0 and 9" . If I want to format the matched value to a

Why is taking the address of a temporary illegal?

你说的曾经没有我的故事 提交于 2019-11-28 09:09:08
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 two objects that are located in the exact same location of memory and avoid the copy." According to the