News for hackers. Stuff that matters.
Re:C++11 FAQ by JoeAltmaier on Thursday January 26, 2017 @10:42AM (#13492217)
Surely references have intrinsic value to the programmer? They're a pointer that can't be moved or rebased. Other languages have only references, and in C++ you can use that convention and not have any extra confusion.
Re:C++11 FAQ by makecheck on Thursday January 26, 2017 @10:55AM (#13492353)
Except that references still can be bad: invalidated later on by an object deletion, no matter how many rules were enforced to assign the reference to begin with. And in an almost-worse sense, "sometimes" references can prevent this and sometimes not. Just not good enough, while syntactically making it harder to tell that references are being used in function calls and such.
Re:C++11 FAQ by cousin_it on Thursday January 26, 2017 @12:59PM (#13493573)
References are crazy. There's no simple guideline to use them safely. Check this out:

    const string& s = max<string>("a", "b");
    cout << s;
Standard types and functions, const references, no warnings, everything by the book. But it crashes.

There's a similar can of worms related to slicing (treating Derived* as Base* is silently unsafe because arrays and copy constructors exist). C++ is scary.

Re:C++11 FAQ by qcoh on Thursday January 26, 2017 @02:04PM (#13494239)
Works for me in both C++03 and C++14? https://ideone.com/KhfGaN
Re:C++11 FAQ by cousin_it on Thursday January 26, 2017 @03:00PM (#13494773)
Your code is not the same as my code and you're probably new to C++. I suggest trying two changes:

1) Replace max("a", "b") with max("b", "a"). Run it, look closely at the result, and be enlightened.

2) Replace max("a", "b") with max<string>("a", "b"). Run it, look at the result, and be enlightened in a different way.

As a small hint, your code invokes unspecified behavior, while my code invokes undefined behavior.

If this exercise doesn't put the fear of C++ into you, then I don't know what will :-)

Re:C++11 FAQ by qcoh on Thursday January 26, 2017 @04:27PM (#13495385)
Sorry, can you clarify? Adding the template argument causes a crash on idone, but I have no idea why that happens. Moreover, the output on my computer is always b\n.

EDIT: Not sure if this is it, but after some digging I've found [0] on lifetimes of temporaries. As for the lacking template argument, presumably const char* is inferred and a temporary string is constructed in main?

[0] http://stackoverflow.com/a/2784304

Re:C++11 FAQ by cousin_it on Thursday January 26, 2017 @06:10PM (#13496245)
So first of all, max("a", "b") expands to max<const char*>("a", "b"). It computes the maximum of two pointers, without looking at the characters at all. Comparing two pointers to unrelated objects is unspecified behavior, but in this case it usually returns the second pointer.

As for max<string>("a", "b"), it implicitly converts both arguments into temporary strings, then returns a const reference to one of them. Since it's a reference to a temporary, it becomes dangling when max returns, but you can assign it to a const string& anyway. When you try to access it, you'll get garbage or crash.

Re:C++11 FAQ by qcoh on Thursday January 26, 2017 @06:15PM (#13496286)
Thank you for the explanation! I'll have to look out for that one...