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? constand visual noiseconstis 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?
constexprvariablesconstexprfunctions
const functions
- Characteristics of
constfunctions constoverloads
const return types
- Returning
constobjects by value - Returning
constreferences - Returning
constpointers
const parameters
constprimitive data typesconstclass type parameters
const and smart pointers
- What is the meaning of a
constsmart pointer? - Should we pass around smart pointers by
const&?
const rvalue references
- The meaning of
constrvalue references - Binding rules
- When to use them
const and templates
- How can
constappear in templates? - Meta-functions handling
constness
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.
