Code generation with CLion
When you have a brilliant idea in mind, you don’t want to be slowed down by something routine and mundane. Wouldn’t it be great if your IDE got those boring and repetitive tasks out of the way so you could develop at the speed of thought?
Well, that is exactly what CLion does for C and C++ developers, with its vast code generation options. Let’s see what they are.
Create from usage
This concept is so simple that you may not believe it’s true when you see it for the first time. Suppose you’ve used a function that is not implemented or not even declared yet. Your code looks red, sure, but what if you press
CLion suggests creating a new function! Press
Enter and voila:
Going forward, let’s assume you’ve started using a variable name, and, for some reason, it doesn’t exist either. Press
Alt+Enter to see what happens:
Plenty of options here! Select between introducing a new local/global variable, a field, a macro or a function parameter. Let’s go with the first option in this example. Now you have a variable declaration and can proceed with coding:
The same naturally works for class names.
Alt+Enter shortcut helps you create declarations and definitions for symbols that are already in use. Actually, it can do even more! Intentions let you apply automated changes to code that is correct, to improve it or to make your coding routine easier. To invoke an intention action, use
Editor | Intentions.
For example, one intention adds parameters and field initializers to the constructor. While introducing a new member, you can add it to the constructor automatically. A signature preview will be shown so you can reorder parameters or add some extras:
There are more intention actions which generate any missing switch case label, or extract the boolean condition from expression and put it to if statement, or apply De Morgan’s laws to a logical expression, or… okay, I’m getting carried away here. The point is, there really are a lot of them, and each can be invoked easily with the simple
Generate getters/setters, constructors/destructors, operators
An intention action can help you add a parameter to a constructor, but what if no constructor is available yet? Or maybe you want your private/protected member to have a getter or a setter. Then what you need is the Generate menu. Press
Cmd-N on OS X) and get them all in no time:
Select the fields to generate getters/setters in this dialog. For class constructors, CLion suggests class fields that should be initialized:
Learn how to generate comparison, relational and stream output operators in CLion (starting from version 2016.2).
Did you notice “Override Functions…” and “Implement Functions…” in the Generate menu above? Choose this menu item, or use
Ctrl+I to implement and
Ctrl+O to override, and CLion will generate stubs for all the selected functions. In C++11, when overriding functions you can use an override specifier:
If you’ve ever wanted to create many lines of code from just a few symbols, you’ll love CLion’s Live Template mechanism. Type
for and press
Tab to get a loop; or type
itit and then press
Tab to iterate over a range:
Even better, create your own Live Templates in Settings/Preferences
Editor | Live Templates! The templates can then immediately be used, shared with your team, or placed in a VCS. To apply a Live Template in code, simply type its abbreviation and press
Tab. If the template includes variables, press
Tab to navigate between them.
To quickly wrap code with a construct, like while, if or #ifdef, use ‘Surround With’ templates by pressing
Ctrl+Alt+T (on Linux/Windows) or
Cmd+Alt+T (on OS X). An expression or a set of statements will be placed inside the selected construct.
As with Live Templates, you can create your own ‘Surround With’ templates. To do that, add a new Live Template that includes a
$SELECTION$ variable, and it will be added to the ‘Surround With’ list:
Thanks to these powerful code generation features in CLion, you can craft code with much less effort.
If you then want to restructure and clean up your code without changing its external behavior, consider using CLion’s code refactorings.
That’s it for today! Browse the Tips’n’Tricks section of this blog to discover other interesting and useful features of CLion.
Subscribe to Blog updates
AI Assistant Insights: Writing C++ Code With the Power of AI in CLion!
AI Assistant in CLion can generate whole code snippets for you. It uses the project context to create relevant function calls and C++ statements.
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…