As Herb and Andrei and I determine the topics we’ll address in our technical sessions at C&B 2012, we’ll post them here. We’ll also post topics we may discuss–prospective sessions. In both cases, we welcome your feedback. What follows is the first session announcement for C++ and Beyond 2012.
Universal References in C++11
T&& Doesn’t Always Mean Rvalue Reference
Perhaps the most significant new feature in C++11 is rvalue references; they’re the foundation on which move semantics and perfect forwarding are built. An rvalue reference is declared like a “normal” reference (now known as an lvalue reference), except you use two ampersands instead of one, i.e., “
T&&” instead of “
T&”. A reasonable expectation would be that if you see a type declaration involving “
&&”, you’re looking at an rvalue reference, but that’s not the case:
int&& var1 = 10; // here, “&&” means rvalue reference auto&& var2 = var1; // here, “&&” does not mean // rvalue reference template<typename T> void f(std::vector<T>&& param); // here, “&&” means rvalue reference template<typename T> void f(T&& param); // here, “&&” might mean // rvalue reference
In this talk, I describe the two meanings of “
&&” in type declarations, explain how to tell them apart, and introduce new terminology that makes it possible to unambiguously communicate which meaning of “
&&” is intended. Distinguishing the different meanings is important, because if you think “rvalue reference” when you see
“ in a type declaration, you’ll misread a lot of C++11 code.
Our journey through DoubleAmpersandVille will include some interesting sights, including glimpses of the three different sets of type deductions rules in C++11, why “
const T&&” does far more than just add
const to “
T&&”, how the token sequence “
T is a template type parameter) can have completely different meanings in different function templates, and what
decltype has in common with
auto and templates. It’ll be a fun, illuminating ride, and by the time it’s over, you’ll be able to distinguish rvalue references from their syntactic twins with aplomb.