#op<<()
23 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
the typical operator<< for a "stream" takes as lhs argument a stream, and returns a reference to that stream
so in terms of "intended" effect there shouldn't be any observable difference between chaining multiple << vs splitting them across two explicit invocations on the same object
but this is just convention/typical implementation, if you start doing funky things, there's nothing that prevents you from returning a completely different object/type/stream after the first <<, at which point you'd have to go and look at whatever the implementation is doing
Look pretty same-ish to me: https://godbolt.org/z/bPrs4dM98
The only potential difference is if you do std::cout << some_funky_type
Whatever the difference between mov rdi, rax and mov edi, OFFSET FLAT:_ZSt4cout are.
Since it could eg have an overload special cased for particular types of ostream, or it might return a different object then you passed in
But this would generally be regarded as a bad idea; in a perfect world there should be no difference
In theory they are not the same. operator<< returns an std::ostream& which is not the same as std::cout, but the compiler seems to be able to see through that.
To be fair, std::cout is also just an lvalue of type std::ostream: http://eel.is/c++draft/iostream.syn
I still wish compilers would optimize both to std::cout << "12"; minus the length check, but I guess that's too much to ask.
firstly, thank you for teh godbolt, and i apologize for being too lazy to have done it myself. but that's quite interesting result. they do produce different code. and different length code, so may perform differently.
fairly sure someone will come up with a silly corner case that prevents that, but yeah, that would be nice.
only thing i can think of, is that sometimes i break up very long strings, at common word boundaries, because i think string-pooling optimizations will have to store less static strings in the binary.
@polar owl Has your question been resolved? If so, run !solved :)
the produced code there is nearly identical
the "only" difference is that in one case, for the second << you pass in cout explicitly, and in the other you pass in whatever the previous call gave you
that, + one swap around of instructions
the "difference" about what stream to use has already been mentioned in text
You can customise the output format and locale and associated numeric facets dynamically which will change how the number is printed
Which would mean that to optimise this the compiler would need to be able to prove you haven't modified any of these things