Declarations in C++
Contents
Declarations in C++¶
I used to spend a lot of time reading and watching conferences on the C++ programming language, and learned a lot about efficiently declaring functions in the modern C++20 compiler. In these notes I hope to explain my understanding of different concepts in declarations.
Table of Contents¶
Method = 0
¶
Pure virtual
Constructor or destructor = default
¶
Using noexcept
¶
volatile
¶
Compilers often try to make many optimizations to the written code. An example is, if no attempt is made to modify some integer num
, it may be try to change
while(num == 0) {
// ...
}
to just
while (true) {
// ...
}
to save having to retrieve and make the comparison (up to two operations). The compiler doesn’t always ‘see’ all the code though, and a modification to num
may be made outside of the code (e.g. in asynchronous or multi-threaded code). In this case, we can use the volatile
keyword to tell the compiler not to optimize code involving this value
volatile int num = 0;
Note that structs declared as volatile
extends to all of the members also (see a SO thread here).