Tips & Tricks

AI Assistant Insights: Writing C++ Code With the Power of AI in CLion!

Read this post in other languages:

CLion can help complete your C++ code in a variety of ways:

  • Basic code completion offers completion options for names, types, and keywords within the visibility scope as soon as you start typing them.
  • Smart type-matching code completion shows only the types applicable to the current context.
  • Statement completion helps you create syntactically correct code constructs by generating and enabling you to insert necessary syntax elements.
  • Postfix completion reduces backward caret jumps by transforming an already-typed expression into a different one based on a postfix you type after the dot, the type of expression, and its context.

With the new AI Assistant, the possibilities are now virtually endless. It really knows your project and its context, and can thus suggest whole new lines of code to you! All you have to do is press Enter and wait a few seconds. The IDE will talk to the LLM model and see if it has any relevant suggestions to provide. The suggestion is shown as gray text and can be applied by pressing Tab.

Inline completion

Inline code completion

We call it inline code completion, and it inserts whole code snippets after the cursor. Doesn’t it look handy? You can evaluate it in CLion v2023.3.3 or later versions by getting an AI Assistant license or using a free trial.

DOWNLOAD CLION

Since the context is known to AI Assistant, it can successfully guess exactly what you’re trying to do. For example, it can help you launch Qt-based apps:

Run Qt app

You can disable automatic suggestions in Settings/Preferences | Tools | AI Assistant. The suggested completion will still be available via the completion menu, a dedicated action, or a shortcut:

AI completion settings

You can also generate all the code at once via the Generate Code… option in the AI Actions context menu:

Run Qt app

AI-based completion in CLion is a powerful tool that can save you time and effort. However, it currently uses OpenAI’s GPT-4 and GPT-3.5 models, as well as models trained by JetBrains. The latest cutoff point for these models is 2021, so some constructs specific to C++20 and C++23 are not known to the service. AI Assistant can also lack some knowledge of C++17’s contexpr.

Give AI-powered code completion a try in CLion! If you want to provide any feedback about it, please do so in the comments or via the issue tracker.

While we’re still on the topic of completion, there’s also a Full Line Code Completion plugin. It utilizes a deep learning model and runs on your local device without sending anything over the internet, which means it is available even when you’re offline. This is provided directly by the plugin, but it currently only supports Python, Kotlin, JavaScript/TypeScript and their associated frameworks, CSS, Java, PHP, and Ruby. We hope to get C and C++ added to this list sometime soon!

Rewriting code with algorithms

In C++, the algorithms library was introduced to help C++ developers search, sort, and manipulate elements of containers. AI Assistant understands when the code can be improved and made more efficient by using <algorithms>:

Introduce algorithms

Other cases where AI Assistant can help

AI Assistant can explain code, answer questions about code fragments, generate documentation and commit messages, and much more. With the help of LLMs, you can complete many programming tasks. For example, fill an array with random numbers:

Generate random numbers

Or link your application with the OpenMP library:

Introduce OpenMP to CMake

And benefit from parallelizing some functions with OpenMP in your code:

Introduce OpenMP

Furthermore, AI Assistant is really helpful when it comes to explaining code. It can serve as a knowledge base about open-source libraries, so you can ask it how to do all sorts of things.

Have you tried AI Assistant in CLion? Share your thoughts and ideas on how to improve it here in the comments or on our issue tracker

Give AI Assistant a try: Code completion for C++ has been available since CLion 2023.3.3, and AI Assistant is coming to CLion Nova soon!

DOWNLOAD CLION

image description