I noticed the BrokenPromise definition in facebook folly::future library, I cannot understand the purpose of the explict BrokenPromise(const char* type) constructor here? is it necessary?
class FOLLY_EXPORT BrokenPromise : public PromiseException {
public:
explicit BrokenPromise(const std::string& type)
: PromiseException("Broken promise for type name `" + type + '`') {}
explicit BrokenPromise(const char* type) : BrokenPromise(std::string(type)) {}
};
https://github.com/facebook/folly/blob/master/folly/futures/Promise.h#L47
1 argument constructors are conversion constructors. If the operation isn't a mere reinterpretation (and lossless at that) most coding standards say you make it explicit.
A BrokenPromise of a string is not a mere lossless reinterpretation of a string. Thus, explicit.
There are other reasons to avoid implicit conversion; for example,
BrokenPromise
could be constructed from0
accidentally ifchar const*
was implicit.A case for non-explicit might be a construcing a complex number from a single float; the reals are a subset of the complex.