Most C++ constructors should be marked explicit
to prevent unintended implicit conversions. So a class Book
containing fields name
and author
shouldn’t be directly initialised from {"hamlet", "shakespeare"}
. It only makes sense for structs that are bags of data.
What is a bag of data?
Structs like pair, tuple, vector are bags of data. They are simply aggregates and nothing else. Implicit construction makes sense for them.
Make bool conversion operator explicit
Ideally you shouldn’t write bool conversion operators, but if you do, you should make it explicit. explicit operator bool() const
will allow things to be used as if
conditionals but prevent bool b = obj
assignments.