我注意到facebook folly::future库中的BrokenPromise定义,我不能理解这里显式BrokenPromise(const char* type)构造函数的用途?有必要吗?
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
发布于 2018-06-01 12:08:16
%1参数构造函数是转换构造函数。如果操作不是简单的重新解释(而且是无损的),大多数编码标准都会说你把它说得很清楚。
字符串的BrokenPromise不仅仅是对字符串的无损重新解释。因此,显式。
避免隐式转换还有其他原因;例如,如果0是隐式的,则可能会意外地从char const*构造BrokenPromise。
非显式的情况可能是从单个浮点数构造一个复数;实数是复数的一个子集。
https://stackoverflow.com/questions/50635916
复制相似问题