why does folly::future BrokenPromise make the const char* constructor explicit?

529 Views Asked by At

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

There are 1 best solutions below

1
On

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 from 0 accidentally if char 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.