简体   繁体   English

有什么正当理由使一元运算符超载?

[英]What legitimate reasons exist to overload the unary operator&?

Okay, I've been inspired to do some head punching . 好吧,我受到启发,做了一些冲击 Seems like overloading operator& leads to not a small amount of pain. 似乎超载operator&导致不小的痛苦。

What legitimate cases exist for overloading it? 重载它有哪些合法案例?

(Can't say I've ever done that....) (不能说我曾经那样做过......)

I seem to remember something like a smart pointer class which overrode operator& because it wanted to return the address of the contained pointer rather than the address of the smart pointer object. 我似乎记得像智能指针类一样覆盖operator&因为它想要返回包含指针的地址而不是智能指针对象的地址。 Can't remember where I saw it or whether it seemed like a good idea at the time. 不记得我在哪里看到它或者当时看起来是个好主意。

Aha, remembered: Microsoft's CComPtr . 啊,记得:微软的CComPtr

Edit: To generalize, it might make sense under the following conditions: 编辑:为了概括,在以下条件下可能有意义:

  • You have an object which is masquerading as some other object. 你有一个伪装成其他对象的对象。
  • This object can obtain a pointer to the thing it's masquerading as. 这个对象可以获得指向它伪装成的东西的指针。

Returning anything other than a legitimate pointer would violate the principle of least astonishment . 返回合法指针以外的任何东西都会违反最不惊讶原则

在用lambda占位符表示法表示&操作时很有用,例如&_1[_2]

Overloading unary & makes your object behave like a reference (in that respect). 重载一元&让你的对象表现得像(在这方面)的参考

I'm pretty sure that it's a fool's errand to attempt to provide alternatives to built-in references, in particular since references aren't objects at all in C++, and they don't have their own addresses. 我很确定尝试提供内置引用的替代方法是一个愚蠢的错误,特别是因为引用在C ++中根本不是对象,并且它们没有自己的地址。 Instances of your user-defined type inevitably are objects, and do have addresses, even if you disable the normal way of obtaining that address. 用户定义类型的实例不可避免地是对象,并且具有地址,即使您禁用获取该地址的常规方法也是如此。 So it is never a perfect imitation. 所以它永远不是一个完美的模仿。

But, people are very keen on user-defined alternatives to pointers, so I can sort of see how someone might want to attempt it. 但是,人们非常热衷于用户定义的指针替代方案,所以我可以看看有人可能想要尝试它。 I'm not sure they'll avoid creating a type that (mis)behaves in ways that will make its users wish they hadn't bothered. 我不确定他们是否会避免创建一种(错误)行为的方式,以使其用户希望他们没有打扰。

Four years later, another answer. 四年后,另一个答案。

Another use I have seen is when you are piggybacking off of the C++ language, but defining your own semantics. 我看到的另一个用途是当你背驮式C ++语言,但定义自己的语义。 Prime example: Boost.Spirit. Prime示例:Boost.Spirit。

Boost.Spirit, in particular Qi for parsing, overloads operators on parsers to provide an EBNF-like syntax for specifying arbitrary parser objects. Boost.Spirit,特别是用于解析的Qi,重载解析器上的运算符,以提供类似EBNF的语法来指定任意解析器对象。 In particular, the unary & operator is overloaded to provide the And-Predicate Parser . 特别是,一元&运算符被重载以提供And-Predicate Parser

And-Predicate Parser (&a) And-Predicate Parser(&a)

Description 描述

Syntactic predicates assert a certain conditional syntax to be satisfied before evaluating another production. 句法谓词在评估另一个生产之前断言某个条件语法要满足。 Similar to semantic predicates, eps, syntactic predicates do not consume any input. 类似于语义谓词,eps,句法谓词不消耗任何输入。 The and-predicate, &a, is a positive syntactic predicate that returns a zero length match only if its predicate matches. and-predicate,&a是一个正的句法谓词,只有在其谓词匹配时才返回零长度匹配。

Example usage: 用法示例:

Basic look-ahead example: make sure that the last character is a semicolon, but don't consume it, just peek at the next character: 基本前瞻示例:确保最后一个字符是分号,但不要使用它,只需查看下一个字符:

test_phrase_parser("Hello ;", lit("Hello") >> &lit(';'), false);

So in short, the unary & here has no relation to pointers at all; 简而言之,一元&这里与指针毫无关系; it has domain-specific semantics which apply to Qi parser objects. 它具有适用于Qi解析器对象的特定于域的语义。

I've done this to good effect in the context of a DSL that generates LLVM code. 我已经在生成LLVM代码的DSL环境中做到了这一点。 An example will illustrate. 一个例子将说明。 Say x and y are values (ie, objects of type value ). 假设xy是值(即类型为value对象)。 Then the expression x+y emits an ADD instruction into some code stream. 然后表达式x+y向某些代码流发出ADD指令。 Quite sensibly, the expression &x emits an instruction to take the address of x . 非常明智地,表达式&x发出一条指令来获取x的地址。

Once I used to override operator & (without altering its behavior) as private to the class, in order to protect against occasional creation of smart pointer to the object created in the stack. 一旦我习惯将操作符&(不改变其行为)重写为类的私有,以防止偶尔创建指向堆栈中创建的对象的智能指针。 Still not sure if it was really good idea... 还不确定这是不是真的好主意......

You can overload the address operator to make it private. 您可以重载地址运算符以使其成为私有。 This could be useful for implementing some sort of baton passing scheme, where the address of the baton cannot be taken. 这对于实现某种接力棒传递方案非常有用,其中不能使用接力棒的地址。 If the baton's constructors are hidden, this can keep the baton's scope airtight. 如果接力棒的构造器被隐藏,这可以保持接力棒的范围不透气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM