KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm thinking about a problem which has some similarity with perfect forwarding, but where the function argument is not passed to a called function, but returned. This is why I call it "perfect pass-through". The problem is the following: Say we have a function which takes an object by reference (and possibly some extra arguments), modifies that object, and returns the modified object. The best-known example of such functions are probably operator<< and operator>> for iostreams. Let's use iostreams as example, because it allows to nicely show what I'm after. For example, one thing which one would sometimes like to do is: std::string s = (std::ostringstream() << foo << bar << baz).str(); Of course that doesn't work, for two reasons: std::ostringstream() is an rvalue, but operator<< takes an lvalue as first argument operator<< returns an ostream& (well, at least for the standard ones actually a basic_ostream<CharT, Traits>& where CharT and Traits are deduced from the first argument). So let's assume we want to design the insertion operator so that the above works (you obviously can't do that for the existing operators, but you can do that for your own classes). Obviously the solution should have the following traits: The first argument can accept either an lvalue or an rvalue. The return type should be the same type as passed in. But of course it should still only accept ostreams (i.e. classes derived from an instantiation of basic_ostream ). While in this specific use case it isn't needed, I want to add a third requirement: If the first argument is an rvalue, so is the returned value, otherwise an lvalue is re
Tags (comma-separated)
Save Edits
Cancel