I am trying to overload
<<
operator. For instance
cout << a << " " << b << " "; // I am not allowed to change this line
is given I have to print it in format
<literal_valueof_a><"\n>
<literal_valueof_b><"\n">
<"\n">
I tried to overload << operator giving string as argument but it is not working. So I guess literal
" "
is not a string. If it is not then what is it. And how to overload it? Kindly help;
Full code
//Begin Program
// Begin -> Non - Editable
#include <iostream>
#include <string>
using namespace std;
// End -> Non -Editable
//---------------------------------------------------------------------
// Begin -> Editable (I have written )
ostream& operator << (ostream& os, const string& str) {
string s = " ";
if(str == " ") {
os << '\n';
}
else {
for(int i = 0; i < str.length(); ++i)
os << str[i];
}
return os;
}
// End -> Editable
//--------------------------------------------------------------------------
// Begin -> No-Editable
int main() {
int a, b;
double s, t;
string mr, ms;
cin >> a >> b >> s >> t ;
cin >> mr >> ms ;
cout << a << " " << b << " " ;
cout << s << " " << t << " " ;
cout << mr << " " << ms ;
return 0;
}
// End -> Non-Editable
//End Program
Inputs and outputs Input
30 20 5.6 2.3 hello world
Output
30
20
5.6
2.3
hello
world
You might want to go with a user defined literal, e.g.
This can be used as follows.
Note three things here:
""_nl
does the same thing as" "_nl
or"hello, world"_nl
. You can change this by adjusting the function returning theNewLine
object.<< ""_nl
is more likely to catch readers' attention than<< " "
.Sure this is possible, as I have tested. It should be portable since you are specifying an override of a templated function
operator<<()
included from<iostream>
. The" "
string in your code is not astd::string
, but rather a C-style string (i.e. aconst char *
). The following definition works correctly:Note that the space after
std::char_traits<char>
is necessary only if you are pre-c++11.Edit 1
I agree with Deduplicator that this is a potentially dangerous solution as it may cause undesirable consequences elsewhere in the code base. If it is needed only in the current file, you could make it a static function (by putting it within an unnamed namespace). Perhaps if you shared more about the specifics of your problem, we could come up with a cleaner solution for you.
" "
is a string-literal of length one, and thus has typeconst char[2]
.std::string
is not related.Theoretically, you could thus overload it as:
While that trumps all the other overloads, now things get really hairy. The problem is that
some_ostream << " "
is likely not uncommon, even in templates, and now no longer resolves to calling the standard function. Those templates now have a different definition in the affected translation-units than in non-affected ones, thus violating the one-definition-rule.What you should do, is not try to apply a global solution to a very local problem:
Preferably, modify your code currently streaming the space-character.
Alternatively, write your own stream-buffer which translates it as you wish, into newline.