Re: Taking a reference to a temporary: why illegal
On 3/6/2013 7:53 PM, Chris Stankevitz wrote:
Is there a good reason that the code below is illegal?
a) No. It's an artifact of some other situation and you get to bear
the pain by having your cute one-liner rendered illegal
b) Yes. That code could cause a segfault on hardware due to
assumptions that the compiler makes about things that are too
complicated for you to understand.
c) Well if you don't like it why don't you create your own programming
language you lazy leech.
d) This is not the correct forum for this question, now scram before I
have to get snooty.
e) [your answer here]
It's such an old question that has been asked and answered here and
elsewhere so many times that it's probably not warranted to repeat the
answer, and is better to give you another chance to find the proper
explanation on the Web by yourself. Start with "bind non-const
reference to a temporary C++" or something the like. Dr. Stroustrup
himself has given an answer to this one, I believe. Somewhere, and I
don't recall where. Could be one of his books. Actually, likely.
I'm leaning toward (a).
<shrug> "Cute" has never been the underlying philosophy of C++ design.
Thank you,
Chris
//===
#include <iostream>
#include <sstream>
void PrintValues(std::istream& Stream)
{
std::string String;
while (Stream >> String)
{
std::cout << String << std::endl;
}
}
int main()
{
PrintValues(std::istringstream("Hello, world!"));
return 0;
}
V
--
I do not respond to top-posted replies, please don't ask