作为独立函数的转换运算符

发布于 2024-08-19 10:03:16 字数 156 浏览 7 评论 0原文

为什么C++要求用户定义的转换运算符只能是非静态成员? 为什么不允许像其他一元运算符一样使用独立函数? 像这样的事情:

operator bool (const std::string& s) { return !s.empty(); }

Why does C++ require that user-defined conversion operator can only be non-static member?
Why is it not allowed to use standalone functions as for other unary operators?
Something like this:

operator bool (const std::string& s) { return !s.empty(); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

起风了 2024-08-26 10:03:16

我能想到的原因之一是防止将隐式转换应用于正在转换的事物。在您的示例中,如果您说:

 bool( "foo" );

那么“foo”将隐式转换为字符串,然后您提供的显式 bool 转换将应用于它。

如果 bool 运算符是成员函数,则这是不可能的,因为隐式转换不适用于 *this。这大大减少了歧义的可能性——歧义通常被视为“坏事”。

The one reason I can think of is to prevent implicit conversions being applied to the thing being cast. In your example, if you said:

 bool( "foo" );

then "foo" would be implicitly converted to a string, which would then have the explicit bool conversion you provided applied to it.

This is not possible if the bool operator is a member function, as implicit conversions are not applied to *this. This greatly reduces the possibilities for ambiguity - ambiguities normally being seen as a "bad thing".

轻拂→两袖风尘 2024-08-26 10:03:16

通过将转换运算符保留在类中,您可以让类的作者控制如何转换它(它可以防止用户创建隐式转换)。作为实施者,我认为这是一个优势,因为 隐式转换确实有它的问题

能够将一个对象传递为另一个对象并让它通过转换函数是有区别的。前者传达该对象属于给定类型,而后者向新读者表明这两种类型之间存在差异并且需要进行转换。

By keeping the conversion operator within the class you give the author of the class control of how it could be converted (It prevents users from creating implicit conversions). As an implementer I would consider this an advantage, as implicit conversions does have its issues

There is a difference being able to pass one object as another, and having it to go through a conversion function. The former communicates that the object is of a given type, while the latter shows new readers that there is a difference between the two types and that a conversion is necessary.

养猫人 2024-08-26 10:03:16

无论如何,隐式的用户定义转换都是不受欢迎的。不要使用它们。只是假装他们不在那里。更不用说考虑引入它们的新方法了。

不管怎样,我猜他们不在那里,因为他们可以做足够多意想不到的事情。包含一个新标头,该标头为其他地方定义的类引入了此类转换,可能会导致更令人困惑的错误。

Implicit user-defined conversions are frowned upon anyway. Don't use them. Just pretend that they aren't there. Let alone thinking about newer ways to introduce them.

Anyway, I guess they aren't there because the way they are they can do enough unexpected things. Including a new header which introduces such a conversion for a class defined somewhere else might lead to even more confusing errors.

躲猫猫 2024-08-26 10:03:16

有一组运算符必须重载为非静态成员函数:赋值、下标、函数调用、类成员访问、转换函数。

我想标准委员会或 Stroustrup 只是觉得如果允许从外部将这些非常特殊的行为注入到类中,可能会太混乱。


我想获得答案的最好方法是给作者发电子邮件。

There's a group of operators that have to be overloaded as non-static member functions: assignment, subscripting, function call, class member access, conversion functions.

I guess the standard's committee or Stroustrup simply felt it might be just too confusing if it was allowed to inject these very special behaviors to classes from outside.


I suppose the best way to get the answer would be to e-mail the author.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文