AI Assistant Insights: Writing C++ Code With the Power of AI in CLion!
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
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.
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:
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:
You can also generate all the code at once via the Generate Code… option in the AI Actions context menu:
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
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.
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
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:
Or link your application with the OpenMP library:
And benefit from parallelizing some functions with OpenMP in your code:
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!
Subscribe to Blog updates
Striving For Better C++ Code, Part II: Function Summaries to Speed Up the Data Flow Analysis
This is the second blog post in the series dedicated to Data Flow Analysis (DFA) and its implementation in CLion. Read the first part here: Striving For Better C++ Code, Part I: Data Flow Analysis Basics Striving For Better C++ Code, Part II: Function Summaries to Speed Up the Data Flow Analysi…
Striving For Better C++ Code, Part I: Data Flow Analysis Basics
CLion comes with a built-in data flow analyzer, which runs constantly when you are writing your code and helps improve your code’s quality. It can reveal various code problems that might later lead to runtime issues, security breaches, and other vulnerabilities. Examples of these useful checks are c…
IDE Features Trainer and Other Improvements in CLion 2023.3 EAP4
Brush up your knowledge of essential IDE functionality easily by using the brand-new IDE Features Trainer.