是否会基于其他操作符自动提供任何C ++运算符重载? [英] Are any C++ operator overloads provided automatically based on others?

查看:83
本文介绍了是否会基于其他操作符自动提供任何C ++运算符重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在编写一个int包装器,并且需要提供每个操作符重载.作者必须列出每个人,还是可以根据作者提供的内容自动生成任何内容?编译器可以/是否可以从现有的运算符中推断出任何新的自动定义的运算符?

Say I'm writing an int wrapper and need to provide every single operator overload. Must the author list out every single one, or can it auto-generate any based on what the author has provided? Can/does the compiler infer any new auto-defined operators from existing ones?

如果我定义了operator==,它会自动给我一个operator!=吗?反之亦然?

If I define operator==, does it give me an operator!= automatically? Or vice-versa?

如果我定义了operator++(),是否可以免费获得operator++(int)?反之亦然?

If I define operator++(), do I get operator++(int) for free? Or vice versa?

+=型企业怎么样?是否可以将现有的operator+定义与operator=结合以生成operator+=?从理论上讲应该有可能,但是可以吗?

How about the += type business? Can it combine existing definitions of operator+ with operator= to generate operator+=? In theory it should be possible but does it?

>=<的相同问题,等等,还是我必须完全列出>>>=<=的定义?

Same question for >= to <, etc, or do I have to fully list out definitions for >,>,>=,<=?

推荐答案

在核心语言中,各种运算符是独立的.有些是根据其他定义的,但是如果操作员调用的重载解析失败,则不会尝试用其他操作员来表示该调用.如果需要,程序员可以很容易地表达出来(相反,关闭这种机器可能会更困难).

In the core language the various operators are independent. Some are defined in terms of others, but if overload resolution for an operator invocation fails then there is no attempt to express that invocation in terms of other operators. When that's desired it can easily be expressed by the programmer (the opposite, turning off such machinery, would probably be more difficult).

std::rel_ops 中存在一组关系运算符重载客户端代码可以使用的,由<==定义.

您可以轻松编写一个mixin类,该类提供有关<==或三值compare函数的关系运算符.这就是好奇地重复模板模式的原始动机,称为

You can easily write a mixin-class that provides relational operators in terms of < and ==, or in terms of a tri-valued compare function. That was the original motivation for the Curiously Recurring Template Pattern, called the Barton-Nackman trick.

这篇关于是否会基于其他操作符自动提供任何C ++运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆