That means that Stroustrup couldn't have ditched pointers, but if he wanted to implement operator overloading for value types with same efficiency as C he needed some way to support doing that without using pointers directly: http://www.stroustrup.com/bs_faq2.html#pointers-and-referenc... http://stackoverflow.com/questions/8007832/could-operator-ov...
A lot of the nastiness of C++ comes from trying to maintain C compatibility: Special behavior for POD, most vexing parse, .h/.cc file split, everything preprocessor and many other misfeatures. Of course, there's also the flip side of it: if C++ wasn't a near-superset of C, it would have never become so popular.
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.
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 :-)
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?
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.
I think the main pain with move semantics was that it had to be bolted on to the language later. Consider that factor, they did an amazing job with it.
Compared to using std::auto?
Nah man. std::move is far easier to understand than trying to shove both concepts down operator=. std::unique is simply easier to use than std::auto by any real measurement.
The other advantage is that you can force only move semantics which allows enforcing things like ownership. Adding an operator doesn't destroy this advantage, but it is a bit silly since you're just adding line noise for something that really should be implicit anyway.
Unless you just literally don't like the spelling of "std::move" which seems pretty lame since it should be relatively rare anyway.
That'd invalidate a lot of programs. Just like how the new _ operator in Java is killing a lot of code right now.
Every symbol that can be used... has been used. "Move" is the meaning of the operator in any case. Its no less readable than say... Python's __lshift__ or __add__.
Consider: :=, <-=, <|, $,
Is there a new operator in some upcoming version of the language specification?
I love C++11 and use it every day, but I think putting references in the language was an 'original sin'. They should have stuck with C's model: it's either a value or a pointer to a value. References just added a pile of complexity and pontificating about whether something was being copied, moved, or passed by reference.