Mixing a string literal concatenation, macro constants, std::string concatenation and obscene amounts of typecasting together in a single line is just a convenient way to show that you're comfortable enough with the language and not afraid to make use of all of it's quirks
He could legit take out all the casts, all the pluses (concatenation), remove the equals sign and surround everything after it with curly brackets, and it would do the same thing.
I don't think so. I think in that case you have to use string concatenation or, as a C programmer my mind jumps to sprintf. This could just be
std::string payload_text_string =
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
"To: " TO_MAIL "\r\n"
"From: " FROM_MAIL "\r\n"
"Cc: " CC_MAIL "\r\n"
"Message-ID: [redacted]"
"rfcpedant.example.org>\r\n"
"Subject: SMTP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC5322 */
"The body fo the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
"Check RFC5322.\r\n";
as shown below; he's not actually using any variable names (other than the constants TO_MAIL etc but that's how it is broken up across lines too, string literals in C can legally appear sequentially). I don't know what /u/trBlueJ is going on about braces.
Ah, that's disappointing. Here I thought I'd finally found a way to append an int to a string without calling std::to_string().
As for the braces, as of about an hour ago, I actually do know that one: in C++ 11 (or possibly earlier; all I know is this doesn't work in 98), you can initialize strings using aggregate initialization the same way you can initialize a vector or an array.
std::string payload_text_string =
(std::string)"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n" +
(std::string)"To: " TO_MAIL "\r\n" +
(std::string)"From: " FROM_MAIL "\r\n" +
(std::string)"Cc: " CC_MAIL "\r\n" +
(std::string)"Message-ID: [redacted]" +
(std::string)"rfcpedant.example.org>\r\n" +
(std::string)"Subject: SMTP example message\r\n" +
(std::string)"\r\n" + /* empty line to divide headers from body, see RFC5322 */
(std::string)"The body fo the message starts here.\r\n" +
(std::string)"\r\n" +
(std::string)"It could be a lot of lines, could be MIME encoded, whatever.\r\n" +
(std::string)"Check RFC5322.\r\n";
std::string payload_text_string =
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
"To: " TO_MAIL "\r\n"
"From: " FROM_MAIL "\r\n"
"Cc: " CC_MAIL "\r\n"
"Message-ID: [redacted]"
"rfcpedant.example.org>\r\n"
"Subject: SMTP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC5322 */
"The body fo the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
"Check RFC5322.\r\n";
std::string payload_text_string {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
"To: " TO_MAIL "\r\n"
"From: " FROM_MAIL "\r\n"
"Cc: " CC_MAIL "\r\n"
"Message-ID: [redacted]"
"rfcpedant.example.org>\r\n"
"Subject: SMTP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC5322 */
"The body fo the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
"Check RFC5322.\r\n"
};
There's some edge cases when initializing with = that don't show up with {} so they're generally preferred. Don't believe any crop up in this specific scenario but it never hurts to initialize with curly brackets. Here's s good talk about how C++ initialization is so fucked
This reminds me of reviewing junior c++ code written by new hires without much experience. This stuff makes a lot of sense in other languages, and I can even tell how it evolved, but it's so gross.
I guess, lol. Does the cast actually create an std::string? And also does writing std::string s = "my string" work? It's been a few months since I did C++ and I forget if std::string("my literal") is necessary.
Yes assignment works BUT they're using the + operator for concatenation, which isn't defined for two char*. You need to have at least one std::string first and that requires a cast or constructor call.
Yes the cast works, there's an override that basically returns a new std::string.
Yup, it's this. You add strings to concatenate them in other langauges. I'm guessing that the programmer tried to do that first, and when it didn't work, then added a bunch of casts.
122
u/Baardi Oct 07 '21
He doesn't know he can line break string literals
75
u/AutomaticPotatoe Oct 07 '21
Mixing a string literal concatenation, macro constants, std::string concatenation and obscene amounts of typecasting together in a single line is just a convenient way to show that you're comfortable enough with the language and not afraid to make use of all of it's quirks
/s
38
u/moonlandings Oct 07 '21
Has this person never heard of stringstream?
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 07 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
23
u/sebamestre Oct 07 '21
Hmmmmmmmm
17
u/Cube00 Oct 08 '21
Using a raw C cast with a C++ class is just the icing on the cake.
8
u/nyaisagod Oct 08 '21
std::format can't get here fast enough.
4
u/Sixo Oct 08 '21
This reminds me of reviewing junior c++ code written by new hires without much experience. This stuff makes a lot of sense in other languages, and I can even tell how it evolved, but it's so gross.
3
u/Pixelmod Oct 08 '21
All the casts are useless, all the
+signs are useless, amazing.3
u/crzydim0nd Oct 08 '21
Just use c++ raw string.
2
u/CE4A Oct 25 '21
you could at least constexpr it... geez