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.
Invoke the Generate popup
Use one of the following options:
Press Alt+Insert.
Call
from the main menu.Right-click in the editor and choose Generate from the context menu.
Generate constructors
Select Constructor from the Generate popup.
If the class contains fields, select the fields to be initialized:
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.
Generate getters and setters
Select Getter, Setter, or Getter and Setter from the Generate popup.
Select the fields to generate the getters/setters for:
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.
Generate equality operators
This action generates operator==
and operator!=
functions that will use the selected fields to compare objects of the current class.
Select Equality Operators from the Generate popup.
Select the fields to be used and set addition options:
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.
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.
Select Relational Operators from the Generate popup.
Select the fields to be used and set addition options:
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.
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.
Select Stream Output Operator from the Generate popup. Select the fields to be used.
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.