5

I have observed that almost in all IDEs code completion for Java and C# is better than that for C++. For example, in Netbeans, Java auto-completion is far superior to C++ auto-completion, while in Visual Studio, C# auto-completion is way better that Visual C++.

There are tons of IDEs out there offering very good Java auto-completion, but the same is not true for C++, even though it is the older language.

Is it more difficult to parse C++? If so, why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
enitihas
  • 835
  • 1
  • 9
  • 17
  • 8
    While in its current form this may be closed, I think objective answers discussing the lexical structure of C++ can be given. – nanofarad May 24 '14 at 23:03
  • 3
    people in C++ try to avoid auto completion they like to use their memory instead of getting help. – SSpoke May 24 '14 at 23:04
  • Possible duplicate of [Why does C++ compilation take so long?](http://stackoverflow.com/questions/318398/why-does-c-compilation-take-so-long) [This answer](http://stackoverflow.com/a/318440/69809) summarizes it nicely. – vgru May 24 '14 at 23:05
  • I use C++ and I LIKE auto completion because in a 5-10m LOC code base written by a lot of people I can't fit all of that in to my memory – paulm May 24 '14 at 23:05
  • 3
    @hexafraction, I am looking for an objective answer only, that explains the reason behind why there are not many IDE's that offer C++ auto-completion, considering that it is a major language. – enitihas May 24 '14 at 23:07
  • @paulm if you /have to/, then there's something wrong with the code base. That said, I only avoid completion because it's almost always too slow. YouCompleteMe might be the gamechanger though – sehe May 24 '14 at 23:17
  • 3
    I honestly don't get why anyone would *not* want the awesome type support provided by Visual Studio/R# (albeit focused on language like C#). Automatic completion is only a *small* fraction of what a modern IDE can/should be able to help with. – user2864740 May 24 '14 at 23:36
  • @enitihas While I don't agree that this is a duplicate of the linked question, it does address several differences in the C++ (over say C#/Java) models that make the task much harder to deal with - processing is only a small part of providing the system required to analyze types (and provide auto-completion). – user2864740 May 24 '14 at 23:41
  • @user2864740, Its not about visual studio and Resharper, I agree they both together make a fantastic development environment. Also, I am not trying to rank IDEs by auto completion. There are many many qualities a good IDE should have. The question is about something else. – enitihas May 24 '14 at 23:43
  • @enitihas That was just an example to further bolster the fact that this *is* common in other environments (and I would hate to leave such tooling!); it wasn't to suggest/recommend a particular IDE (e.g I don't even use C++ in VS). – user2864740 May 24 '14 at 23:43
  • @SSpoke hope this is sarcasm :) – M.M May 25 '14 at 00:59
  • @MattMcNabb, I believe the stereotype is that Java programmers press control-space for a few seconds to write their code, then spend a few hours in the debugger trying to figure out why their program doesn't do what they wanted (it was because they called `Integer.getInteger`), whereas C++ programmers think about the problem for a minute, spend a few minutes actually reading the documentation, and then type out code that works the first time. – Samuel Edwin Ward May 25 '14 at 02:07
  • 1
    @sehe no code base is perfect, and even so I could never remember or want to remember the interface of 1000's of classes that wouldn't help me else where. STL yes, but bobs low level string interface? Not so much – paulm May 25 '14 at 11:30
  • @paulm I should hope that if you work with _Bobs low level string interface_, you know it very well, by now. And if that's not the case, it's actually not a ~5M codebase after all, and you work with hundreds of legacy projects, and yes, if each of them uses their own low-level string interface, I think these are problems that Intellisense can only artificially sustain. And I'll say it again, I like autocompletion. R#/C# is the best, I love the refactorings to death. But I'll pass **any day** on the broken, dog slow, sources of misinformation that pass for "autocompletion for c++" in most IDEs – sehe May 25 '14 at 12:39
  • 1
    Any real world large code base tends to have about 5 variants of string::to_lower and such, remembering them all is a waste, and adding your own is adding more duplication, auto complete really helps in these cases (I use VAX and its pretty good). Then most "managers" that have been put in could contain anything, any old large code base has a lot of rusty edges. – paulm May 25 '14 at 14:56

2 Answers2

9

Parsing C++ is more difficult, because the grammar is very stateful. Knowing whether A * b; is a pointer declaration or multiplication depends on whether the identifier A in the current scope refers to a type or variable.

But it's not just parsing. Autocompletion requires semantic analysis, overload resolution, template expansion, selection of template specializations, evaluation of constexpr functions since they can appear in template argument lists...

Basically to determine the type of an arbitrary C++ expression and list the members of that type, you need all of a non-optimizing compiler except the machine code generation.

Most of the above steps don't apply to languages that don't have template specialization.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 3
    Xcode's integration with LLVM supposedly happened so they could use the regular compiler for all of that, yet Xcode's C++ autocomplete is still terrible. – Adam May 24 '14 at 23:46
  • 1
    @Adam: Using the regular compiler for all of that isn't a particular effective choice, since autocompletion is used on a file that's being edited, it is between compilable states. – Ben Voigt May 24 '14 at 23:48
1

The C++ FQA has a wealth of information about this (although it is written in a style that could be described as "very informal").

Here is some of what it has to say about the factors involved:

is AA BB(CC); an object definition or a function declaration? It turns out that the answer depends heavily on the code before the statement - the "context". This shows (on an intuitive level) that the C++ grammar is quite context-sensitive.

...

So let's locate the definition of CC and move on, right?

...

there's no way to tell in which files CC is defined, or which files must be parsed in order to "understand" its definition

And all of that's before we even begin to consider the turing-complete template system which can automatically generate new code at compile time.

Another advantage that Java and C# have over C++ in this area is reflection. Once you've compiled a Java class, you can load it into a JVM (and you know what file it's in because Java specifies these things) and use the standard interface to inquire as to its methods and so forth. C++ does not provide that capability.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
  • 2
    The FQA is also a bunch of garbage. – Ben Voigt May 24 '14 at 23:46
  • 3
    @BenVoigt, can that comment be expanded into a constructive one? – Samuel Edwin Ward May 24 '14 at 23:53
  • 3
    Well, your particular quote isn't *that* bad, although "no way to tell which files must be parsed" is only sometimes true (when editing a header file, for example). The main problem with the FQA is that its goal is to find problems with C++, never solutions. So even when there's a very simple workaround, or actually a good reason for the behavior, the FQA presents only the most negative side of the story. Ultimately, it isn't very informative. – Ben Voigt May 24 '14 at 23:55
  • 1
    @BenVoigt ` it isn't very informative` I disagree, I found it very useful to help me point out some difficult aspects of C++ I didn't even suspected ! It is useful in the sense that it helps getting a better understanding of the language, even though its point is (I agree) heavily biased. – kebs May 25 '14 at 07:29
  • @kebs: Presenting designs as bad when there's actually a very good reason pretty much ruins the information. There's no way to pick out the instances where the FQA has a good point from the many more instances where the FQA author is spreading misinformation. – Ben Voigt May 25 '14 at 11:30
  • @BenVoigt, there's a lot of complicated code in header files, so I'd definitely want information when editing them. Another situation in which it's not possible to tell which files need to be parsed to understand a declaration would be when the programmer hasn't included them (yet). You could even come up with scenarios where a definition has been included, but the one the programmer intends to use hasn't been. – Samuel Edwin Ward May 25 '14 at 13:15
  • @Samuel:those latter cases affect other languages too. Didn't import the right things in Java, or use the right namespaces in C#? Your autocomplete is toast. – Ben Voigt May 25 '14 at 15:22
  • 1
    On the contrary, Java and C# environments are able to quickly suggest import and using statements, because automatically finding the definitions for a name is easy to do. – Samuel Edwin Ward May 25 '14 at 16:03