0

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 Answers1

0

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • thanks, but there is already a explicit const std::string constructor, and a const char* will implicitly convert to const std::string&. so why bother define a const char* constructor? – haipeng Jun 01 '18 at 05:11