From 26fc14cc8a831826c1b9b8ed6245b683d6aa26d1 Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Mon, 21 Dec 2015 12:25:56 -0500 Subject: [PATCH] operator& C.166 Overload unary `&` only as part of a system of smart pointers and references --- CppCoreGuidelines.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 73c11e3..f2579ba 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -1,6 +1,6 @@ # C++ Core Guidelines -December 14, 2015 +December 21, 2015 Editors: @@ -6018,6 +6018,7 @@ Overload rule summary: * [C.163: Overload only for operations that are roughly equivalent](#Ro-equivalent-2) * [C.164: Avoid conversion operators](#Ro-conversion) * [C.165: Use `using` for customization points](#Ro-custom) +* [C.166: Overload unary `&` only as part of a system of smart pointers and references](#Ro-address-of) * [C.170: If you feel like overloading a lambda, use a generic lambda](#Ro-lambda) ### C.160: Define operators primarily to mimic conventional usage @@ -6194,6 +6195,40 @@ This is done by including the general function in the lookup for the function: Unlikely, except for known customization points, such as `swap`. The problem is that the unqualified and qualified lookups both have uses. +### C.166: Overload unary `&` only as part of a system of smart pointers and references + +##### Reason + +The `&` operator is fundamental in C++. +Many parts of the C++ semantics assumes its default meaning. + +##### Example + + class Ptr { // a somewhat smart pointer + Ptr(X* pp) :p(pp) { /* check */ } + X* operator->() { /* check */ return p; } + X operator[](int i); + X operator*(); + private: + T* p; + }; + + class X { + Ptr operator&() { return Ptr{this}; } + // ... + }; + +##### Note + +If you "mess with" operator `&` be sure that its definition has matching meanings for `->`, `[]`, `*`, and `.` on the result type. +Note that operator `.` currently cannot be overloaded so a perfect system is impossible. +We hope to remedy that: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4477.pdf. +Note that `std::addressof()` always yields a built-in pointer. + +##### Enforcement + +Tricky. Warn if `&` is user-defined without also defining `->` for the result type. + ### C.170: If you feel like overloading a lambda, use a generic lambda ##### Reason