Monday, January 7, 2008

rvalue reference


C++ Quiz, Question #42 (Gooroo-Level)

What does the following code do:

    std::ofstream("Log file",std::ios::app) << "Log message\n";

Hint: the obvious answer is wrong.

Answer:
This compiles, but does not do what the programmer intended. The inserter binds to the member const void* inserter, instead of to the non-member const char* inserter. And so the address of the literal is written to the file.
To solve this confusion the author suggests to introduce rvalue references to C++. Although this might solve situations like the above I don't think it is a good idea to add another dimension into the map of the cardesian product of C++ features which has a lot of "undefinded", "unsupported" and "forgotten" entries already.

C++ has done it's job pretty well but it also evolved into being a dead-end. Come on and let's find a language which is comfortable to use and efficient.

(thanks Roker)

No comments:

Post a Comment