Back to Basics
Declarations
While most C++ programmers have a general understanding of how to read and write declarations, some aspects of declarations can confuse even experienced C++ programmers. For example, why does
constexpr int *p1 = addr1;
require an initializer, but
const int *p2;
does not?
As another example, when you write a declaration such as T &&t_ref, how can you tell whether t_ref is an rvalue reference or a forwarding reference?
In this session, we explain what you need to know to answer these and many other questions about declarations. We begin with a detailed look at the structure of declarations. Along the way, we discuss the differences between type and non-type specifiers and how the compiler interprets them. We then show how a declaration’s context affects its meaning, such as
• how the ambiguous nature of dependent names in templates creates the need for the typename keyword to assist the compiler, and
• how a type deduction context alters the meaning of a reference declaration.
You’ll leave this session with a clearer understanding of how the compiler interprets declarations. Armed with this understanding, you’ll find it easier to write declarations that mean what you intend and to resolve compile-time error messages involving declarations.
Ben Saks
Ben Saks is the chief engineer of Saks & Associates, which offers training and consulting in C and C++ and their use in developing embedded systems. Ben represents Saks & Associates on the ISO C++ Standards committee as well as two of the committee’s study groups: SG14 (low-latency) and SG20 (education). He has spoken at industry conferences, including the C++ and System Software Summit, the Embedded Systems Conference, NDC Techtown, and CppCon, where he’s also chair of the Embedded Track and a member of the program committee. Ben previously worked as a software engineer for Vorne Industries, where he used C++ and JavaScript to develop embedded systems that help improve manufacturing productivity in factories all over the world. He’s also a contributing author on multiple Vorne patents.