I was reading this answer today and noticed a plus sign in front of the char arrays, but don't know what does it mean.
Given the compilation error when I remove it I can guess it is something to help the compiler to infer the return type, but I don't know how it works.
Test code (also here):
#include <iostream>
using namespace std;
auto& operator<<(std::ostream& os, const char (&s)[2]) {
return os << (*s == ' ' && !s[1] ? +"\n" : +s);
}
int main() {
cout << "Hello" << " " << "world" << " " << 2018;
return 0;
}
When removing the plus signs (sample) it doesn't compile:
main.cpp: In function 'auto& operator<<(std::ostream&, const char (&)[2])':
main.cpp:6:48: error: use of 'auto& operator<<(std::ostream&, const char (&)[2])' before deduction of 'auto'
return os << (*s == ' ' && !s[1] ? "\n" : s); ^
main.cpp: In function 'int main()':
main.cpp:10:24: error: use of 'auto& operator<<(std::ostream&, const char (&)[2])' before deduction of 'auto'
cout << "Hello" << " " << "world" << " " << 2018; ^~~