I accidentally added a semicolon in an expression constructing std::string, but the compiler happily accepted it without complaining anything.
I did a minimum reproducible example below, kindly teach why the statement seems to be deemed valid.
#include <iostream>
int main() {
std::string msg;
msg += std::string("15") + "nm"; +" <= " + std::string("limit");
}
Reproducible by GCC12.3 on RHEL and MSVC v19.37 on Windows.
+ " <= " + std::string( "limit" );
is parsed as
(+" <= ") + std::string( "limit" );
and +" <= "
"forces" the conversion of const char[5]
into const char*
.
From Built-in_unary_arithmetic_operators (emphasis mine)
- For the built-in unary plus operator, expression must be a prvalue of arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on expression if it has integral or unscoped enumeration type. The type of the result is the (possibly promoted) type of expression. The result of the built-in promotion is the value of expression.