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
Thanks, we've got you!
Introducing the New CLion Conan Plugin
We are thrilled to announce the launch of the new version of the Conan CLion Plugin, now compatible with the 2.X versions of Conan.
How To Address 7 Major C++ Pain Points with CLion
The top major C++ pain points have remained unchanged in recent years. How can a modern IDE help address these pain points?
How To Debug C++ Code Efficiently: Disassemble On Demand
CLion 2023.1 brought the ability to explore assembly code even when the source code is available. A new Disassemble action has been added to improve your debugging experience, help you catch issues, and eliminate inefficiencies in the code. When debugging C or C++ code in CLion, you can explore th…