How to use correctly the const qualifier?
Sandor Dargo
We all use const in our code. But why? Do we use it enough or too much? Do we use it well?
This workshop will provide the answers to those questions! We'll discuss why we need to bring constness to our code and why we need more of it. We'll go into detail to see all the different contexts where const can appear. With the help of examples, we'll examine where it helps, where it's ignored and where it's even harmful!
Why using const
matters and common arguments against using const?
- Should we make everything
const
? const
and visual noiseconst
is confusing for the developer- It doesn’t matter anyway
const
local variables
- How do they help?
- How to
const
-initialize from multiple options?
const
member variables
- Why would you want to have const members?
- Some unexpected implications
- How (not) to use
const_cast
?
What is constexpr
?
constexpr
variablesconstexpr
functions
const
functions
- Characteristics of
const
functions const
overloads
const
return types
- Returning
const
objects by value - Returning
const
references - Returning
const
pointers
const
parameters
const
primitive data typesconst
class type parameters
const
and smart pointers
- What is the meaning of a
const
smart pointer? - Should we pass around smart pointers by
const&
?
const
rvalue references
- The meaning of
const
rvalue references - Binding rules
- When to use them
const
and templates
- How can
const
appear in templates? - Meta-functions handling
const
ness
Wrap up
Sandor Dargo
Sandor is a passionate software craftsman focusing on reducing the maintenance costs by developing, applying and enforcing clean code standards. His other core activity is knowledge sharing both oral and written, within and outside of his employer. When not reading or writing, he spends most of his free time with his two children and his wife baking at home or travelling to new places.