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 Alt+Enter?
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.


The 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 Alt+Enter. The list of all available intention actions (for C and C++, as well as for JavaScript, XML and CSS) can be found in Settings/Preferences in 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 Alt+Enter shortcut.

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 Alt+Insert (or 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).

Implement/Override/Generate Definitions

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:

Learn also about Generate Definitions possibility and it’s adaptive behaviour introduced in CLion 2016.2.


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 iter or 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 Enter or 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.

This entry was posted in Tips'n'Tricks and tagged , . Bookmark the permalink.

12 Responses to Code generation with CLion

  1. Petter Strandmark says:

    Seems to have some issues with templated classes.

  2. al3abnat says:

    Thanks to these powerful code generation features in CLion, i can create many lines of code from just a few symbols.

  3. Michael Chadwick says:

    Any way to generate a derived class from a base class?

    • Anastasia Kazakova says:

      No, however there is an extract subclass refactoring available.
      What do you expect from generating derived class?

  4. Santhosh K S says:

    How to change the get set method names

    for example get set method creation auto generates the code like
    “getCurrentTime()” where as i want to keep my method name starting with upper case
    “GetCurrentTime()” notice the “g” changed to “G” in my case. How to do this?

    • Anastasia Kazakova says:

      Go to Editor | Code Style | C/C++, Code Generation tab, and set Get/Set as prefixes for getters/setters.

  5. Brent Huang says:

    There is a class named AllowedOpSet, and I define a instance of that class. The auto complete instance name is allowedOpSet, but I want allowed_op_set. Can you help me?

  6. Bob says:

    Can you please add hash function generation like intellij?

Leave a Reply

Your email address will not be published. Required fields are marked *