Skip to content
randomphrase edited this page Mar 5, 2014 · 5 revisions

One of the main reasons for using Emacs in the first place is to take advantage of its power as a text editor, and many of these generic functions are just as useful with C++ code as with other languages. However there are some capabilities which are particularly useful for coding C++, and the ones which I most rely on are described here.

Completion

Needless to say, completion is incredibly useful. The idea is that the tools work out whether completion is possible at a given point in the code, and if so present a list of possible alternatives, hopefully guided by the current context. Note that in the example below, the private method is not presented as an alternative because it cannot be invoked outside of the class.

images/completion.png

There are several methods of getting this working, but I find company mode to be the simplest to configure, and minimally intrusive in practice. Basically all you do is enable it and the default completion sources will kick in to provide completion in most cases.

(autoload 'company-mode "company" nil t)
(global-company-mode)

It also provides a quick reference for any associated documentation (see the minibuffer in the screenshot).

Code Completion

For C++ source files, company hooks into Semantic, which we have enabled previously (see Opening Projects), and so works without any additional configuration.

Of course, there are some fundamental limitations with code completion in C++. Particularly with generic programming techniques, where “duck typing” is common, code completion is not generally possible. This situation might change in future iterations of C++ where we have concepts, but in the current language it is not generally possible.

In addition, there are several practical limitations with the Semantic C++ parser, which are exposed via code completion. For example, there is currently no completion available for template arguments such as std::vector<....

For more challenging completion candidates, the state of the art is probably provided by Clang. Company mode does provide a completion source for clang, but it requires a bit of configuration. To date, I haven’t really got it working, but I’ll keep trying.

CMake Completion

Another awesome feature of company is that it provides completion for CMake commands and variable names. This works without additional configuration in cmake-mode buffers.

Header File Completion

TODO: Need to write a completion backend for this one, similar to ac-c-headers but for company.

Clone this wiki locally