[英]Does it ever make sense to overload unary operator &?
So, C++ allows overloading the unary operator &
(address). 因此,C ++允许重载一元运算operator &
(地址)。 Are you aware of any real-world example when operator &
was rightfully overloaded? 当operator &
被正确重载时,您是否了解任何实际示例? And a second, more specific question, are you aware of any real-world example when operator &
was rightfully overloaded while preserving address semantics? 第二个更具体的问题是,在保留地址语义的同时,当operator &
被正确重载时,你是否知道任何现实世界的例子? TIA TIA
I've got 207 real-world examples of operator &()
: Code search 1 , Code search 2 . 我有207 个 operator &()
真实示例: 代码搜索1 , 代码搜索2 。
Including SafeInt<>
(to get the underlying naked integer), boost::gil (apparently also to yield the raw data), Mozilla (that say "it is risky to define operator&, but, hey, we know what we're doing."), wxWidgets, Armagetron and lots of more. 包括SafeInt<>
(以获取底层裸整数), boost :: gil (显然也是为了产生原始数据),Mozilla(说“定义运算符有风险,但是,嘿,我们知道我们在做什么。),wxWidgets, Armagetron等等。
It seems some use the iterator idiom &*it
to get a raw reference or pointer backwards, and write *&it
to get a raw reference and &it
to get a raw pointer. 似乎有些人使用迭代器习语&*it
来向后获取原始引用或指针,并编写*&it
以获取原始引用和&it
以获取原始指针。
Notice that once your type overloads operator&
and returns something different than the built-in operator, your type is not CopyConstructible anymore (in C++03 - C++0x seems to have lifted it), and so cannot be used as element-type in a Standard container anymore. 请注意,一旦你的类型重载operator&
并返回与内置运算符不同的东西,你的类型就不再是CopyConstructible了(在C ++ 03中 - C ++ 0x似乎已经解除了它),因此不能用作元素 -再输入标准容器。
I don't know of a concrete example off-hand, but I could imagine a container class where you might want to return a smart pointer or an iterator. 我不知道一个具体的例子,但我可以想象一个容器类,你可能想要返回一个智能指针或迭代器。 I'm not saying this necessarily makes sense, though. 不过,我并不是说这必然是有道理的。
One good reason to overload it might be to make it private
, to prevent users from using it. 超载它的一个好理由可能是将其设为private
,以防止用户使用它。 I can't this think of any real-world example where you would want to prevent this, but it seems to be the most logical reason to overload it. 我不能想到你想要阻止它的任何现实世界的例子,但它似乎是超载它的最合理的理由。
I did it once when an object had a special-purpose smart pointer. 当一个对象有一个特殊用途的智能指针时,我做了一次。 operator&
quietly 'lifted' a stack-allocated object into a heap-based smart pointer version, and this operator behaved differently once the object was inside the pointer. operator&
悄悄地将堆栈分配的对象“提升”为基于堆的智能指针版本,并且一旦对象在指针内部,此运算符的行为就会不同。
I don't have the code any more, but there was a reason for it at the time. 我不再有代码,但当时有理由这样做。 It's certainly not a decision to take lightly, this road is lined with corpses. 这显然不是一个轻率的决定,这条道路上布满了尸体。
I overloaded this operator when writing classes for interacting with Direct3D. 在编写用于与Direct3D交互的类时,我重载了此运算符。 It was a smart pointer class that needed to have T** returned from operator& so that it could be used in functions that expect pointer-to-pointer. 这是一个智能指针类,需要从运算符返回T **,以便它可以用于期望指向指针的函数。 T** semantics are rare but you do need them in some situations. T **语义很少见,但在某些情况下确实需要它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.