I am using Eclipse 4.12.0 and Eclipse CDT 9.8.1 and I am writing C++ code that uses std::tuple
. Sometimes the code formatter introduces extra spaces in my code where I retrieve entries of tuples.
Please consider the following example file:
#include <tuple>
int main(int, char**)
{
std::tuple<int, int> myTuple; // First important line
std::get<0>(myTuple) = 2; // Second important line
return 0;
}
I have pasted here the code formatted by Eclipse. Everything is fine in this example. But when I copy and paste the two lines marked as important by the comments into another .cpp
file of my project and format them, the results is this:
std::tuple<int, int> myTuple; // First important line
std::get < 0 > (myTuple) = 2; // Second important line
The formatter has introduced spaces before and after the template angle brackets as if I would be doing a "greater than" or "lower than" comparison. This effect seems to somehow depend on the project. If it happens within a source file of one project in my project explorer, it seems to happen within every source file of that project. But as soon as I paste the lines into a source file of another project, the code formatter behaves correct (like in the example file given above).
I have not reported this as a bug, since I do not know how to make it reproducible. Does anyone have an idea what the reason might be? I have not enabled project specific code formatter settings, therefore it should not be a simple configuration error. Also adding or removing the line #include <tuple>
does not make a difference. What else could I try?
The proximate cause of the issue is almost certainly that Eclipse is mis-parsing the code and thinks the <
and >
characters are actually comparison operators in the case where it puts spaces around them.
As for why it mis-parses the code? C++ is a hard language to parse and CDT's parser hasn't been keeping up with new C++ language versions (nor has it ever been fully accurate even for older C++ versions).
The fact that "if it happens within a source file of one project, it seems to happen within every source file of that project" suggests that the mis-parse occurs inside a header file whose parsed representation is shared by all files in a project.
If formatting is your only issue with Eclipse CDT, I recommend the CppStyle plugin. That uses a clang-based formatter which has no problems parsing C++ correctly.