tl;dr
I want to understand the logic by which I can make the following comments about a non-working code:
- Oh, it's obvious it fails, there's syntactic error there at line 5
- Mmm, look, I think between lines 10 and 13 there's a semantic error
- No, you cannot call a variable
a±b
, that's a lexical error - At line 50 there's a X error, for any X type of error beside semantic and syntactic.
Original question
I add the C++ because I use an example from this language, and the language-lawyer tag in case the answer is language-dependent, even though my question is more general.
If I write this code in C++,
x x x;
it won't compile with error like this
$ g++ -std=c++17 uffa.cpp && ./a.out
uffa.cpp: In function ‘int main()’:
uffa.cpp:4:6: error: expected ‘;’ before ‘x’
4 | x x x;
| ^~
| ;
My understanding is that x x x;
is just syntactically wrong; ill-formed is a better term? Or syntactically ill-formed?
On the other hand, code this
s.append("!");
can be valid, e.g. if it is preceded by std::string s{"Ciao"};
, or invalid, e.g. if preceded by int s{3};
, in the latter case the error being like this
$ g++ -std=c++17 uffa.cpp && ./a.out
uffa.cpp: In function ‘int main()’:
uffa.cpp:5:7: error: request for member ‘append’ in ‘s’, which is of non-class type ‘int’
5 | s.append("!");
| ^~~~~~
Despite the errors have the same "shape", my understanding is that this latter error has to do with the code being invalid at a different "level" than it is in the first example. Is it semantically invalid/incorrect/ill-formed?
How does this "classification" of errors relate to SFINAE and if constexpr
?