chromium / subspace

A concept-centered standard library for C++20, enabling safer and more reliable products and a more modern feel for C++ code.; Also home of Subdoc the code-documentation generator.
https://suslib.cc
Apache License 2.0
89 stars 15 forks source link

Prevent implicit temporary binding to const& in const&-holding types #290

Closed danakj closed 1 year ago

danakj commented 1 year ago

A function receiving const T& and passed a U that is convertible to T will implicitly create a temporary T and pass the reference to that.

But if the function is going to store the reference, then this means it holds a pointer to a temporary. The Google style guide just forbids passing a const& at all in this case.

We use sus::construct::SafelyConstructibleFromReference to prevent implicit conversions at the caller, and apply this to the with() constructor methods on the three types that hold references: Option, Result, and Tuple.