CLion 2024.2 Help

Code generation

CLion provides multiple ways to generate common code constructs and recurring elements, which helps you increase productivity. These can be either file templates used when creating a new file, custom or predefined live templates that are applied differently based on the context, various wrappers, or automatic pairing of characters.

The Generate menu helps you quickly create standard code constructs in your code. Depending on the current context, the caret position, or selected code, CLion gives you a list of constructs to be generated.

the Generate menu

Invoke the Generate popup

Use one of the following options:

  • Press Alt+Insert.

  • Call Code | Generate from the main menu.

  • Right-click in the editor and choose Generate from the context menu.

Generate constructors

  1. Select Constructor from the Generate popup.

  2. If the class contains fields, select the fields to be initialized:

    Generate constructor
  3. If the Generate in-place checkbox is selected, the template constructor will be generated in the public area of the class, otherwise the code will be placed in the corresponding cpp file.

    Constructor generated in-place

Generate getters and setters

  1. Select Getter, Setter, or Getter and Setter from the Generate popup.

  2. Select the fields to generate the getters/setters for:

    Generate getters and setters
  3. If the Generate in-place checkbox is selected, the getters/setters will be generated in the public area of the class, otherwise the code will be placed in the corresponding cpp file.

    Getters and setters generated in-place

Generate equality operators

This action generates operator== and operator!= functions that will use the selected fields to compare objects of the current class.

  1. Select Equality Operators from the Generate popup.

  2. Select the fields to be used and set addition options:

    Generate equality operators
  3. If the Generate in-place checkbox is selected, the operator functions will be generated in the public area of the class, otherwise the code will be placed in the corresponding cpp file.

    Equality operators generated in-place

Generate relational operators

This action generates operator<, operator>, operator<=, and operator>= functions that will use the selected fields to compare objects of the current class.

  1. Select Relational Operators from the Generate popup.

  2. Select the fields to be used and set addition options:

    Generate relational operators
  3. If the Generate in-place checkbox is selected, the operator functions will be generated in the public area of the class, otherwise the code will be placed in the corresponding cpp file.

    Relational operators generated in-place

Generate stream output operators

This action generates the insertion operator operator<< that will use the selected fields to define how to generate stream output for objects of the current class.

  1. Select Stream Output Operator from the Generate popup. Select the fields to be used.

  2. If the Generate in-place checkbox is selected, the operator function will be generated in the public area of the class, otherwise the code will be placed in the corresponding cpp file.

    Stream output operator generated in-place
Last modified: 11 October 2024