[libcxx-commits] [PATCH] D119894: [libc++] Add `explicit` to a bunch of private detail ctors.
Arthur O'Dwyer via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 21 11:19:33 PST 2022
Quuxplusone added inline comments.
================
Comment at: libcxx/include/__debug:156
- __libcpp_db();
+ explicit __libcpp_db();
public:
----------------
Mordante wrote:
> Just curious, but is there a good reason to make a default constructor explicit?
Just the usual: it prevents implicit conversions.(*)
- `explicit Foo(int, int)` prevents `void f(Foo); f({1,1});`
- `explicit Foo()` prevents `void f(Foo); f({});`
My personal rule is "Make //every// ctor explicit unless you intend it to be called implicitly (which should be rare because implicit conversions are confusing)." The exceptions of course are the copy and move ctors, because we do intend them to be called implicitly all over the place.
(* - pedantic note: the "conversion" from a braced initializer list is not technically a conversion, because the braced initializer list doesn't have a type to begin with. But it's the same idea, both to a human who thinks of `{1,1}` as "a pair of ints" and to the compiler who thinks of it as a context requiring a non-explicit ctor.)
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119894/new/
https://reviews.llvm.org/D119894
More information about the libcxx-commits
mailing list