私人朋友操作符的代理类< [英] proxy class for a private friend operator<<
问题描述
所以昨天我问这个问题私人朋友运营商<<
如何使运算符<<
私有的类。我得到了一个非常好的答案,
它完全按照我想要的,但我有一些额外的问题。
首先,我不明白那么代理类不必是 private_printable
的朋友呢?我如何可以从运算符<<
?中访问 private_printable
的内部成员?
其次,在使用回答代码时,我写了:
proxy()const {return * this; }
首先,它似乎对我和我甚至编译,但是当我运行它, segfault
我意识到我没有为代理类定义任何构造函数,
将 private_printable
作为参数,因此允许转换。
那么,我的代码如何编译,甚至没有给我任何警告?
然后我做的第三件事是给代理一个构造函数它有一个引用
到 private_printable
作为其参数(我没有使它显式),而不是定义转换操作符。
现在一切顺利,除了可以再次调用运算符<<
在 private_printable
。但我不太确定,为什么会这样。
是因为ADL吗?我对它有一些模糊的理解,但我不是
确定其所有的细节。无论如何,代理
类是私有的,所以为什么
ADL会有什么区别?
-
运算符<<
可以访问private_printable
,因为它是private_printable
的朋友
。它不是代理
的朋友
。 -
return * this
返回一个private_printable
对象,必须隐式转换为code>。如何将
private_printable
转换为代理
?调用private_printable :: operator proxy()
。但这是我们已经在的功能! segfault是由无限递归的堆栈溢出引起的。 -
非 -
显式
构造函数也是一个转换函数,服务于运算符代理
的相同目的。 ADL是一种涉及,就像使用运算符代理
,没有构造函数。解决方案当然是使构造函数显式
。
这个问题只发生在特定情况下,构造函数的唯一操作数是代理类。 代理
类是私有
成员只有在明确命名时才有意义。 ADL仍然可以找到一个带有代理
参数的函数,然后间接引用对转换函数的访问,因为类类型的使用不受访问限制的影响。只有名称查找受影响。同样,如果你只有一个 typedef
,你总是可以使用私人
成员类型。
So yesterday I asked this question private friend operator<<
about how to make operator<<
private for the class. I got a very good answer,
which worked exactly as I would like, but I have some additional questions about it.
First, I don't understand how come that the proxy class does not have to be friend of private_printable
? How is it possible that I have access to internal members of private_printable
from within operator<<
?
Second, while playing with the code from answer, I wrote this:
operator proxy () const { return *this; }
At first it seemed OK to me and it even compiled, but when I run it and got a segfault
I realized that I haven't defined any constructor for the proxy class,
that would take private_printable
as argument and thus allow the conversion.
So how is it possible that my code compiled, without even giving me any warnings ?
Then the third thing I did, was to give the proxy a constructor that had a reference
to private_printable
as its argument (I did not make it explicit) instead of defining conversion operator.
Now all went fine except that it was again possible to call operator<<
on private_printable
. But I am not really sure, why this happened.
Is it because of ADL ? I have some vague understanding about it, but I am not
sure of all its details. Anyways, the proxy
class is private, so why would
ADL make any difference ?
The
operator<<
can access members ofprivate_printable
because it's afriend
ofprivate_printable
. It's not afriend
ofproxy
.return *this
returns aprivate_printable
object, which must be implicitly converted toproxy
. How to convertprivate_printable
toproxy
? Callprivate_printable::operator proxy ()
. But that's the function we're already in! The segfault is caused by stack overflow from infinite recursion. For what it's worth, an endless loop would be another possible outcome.The non-
explicit
constructor is also a conversion function, serving the same purpose asoperator proxy
. ADL is sort-of involved, the same as if theoperator proxy
is used and no constructor. The solution of course is to make the constructorexplicit
. And the problem only occurs for that specific case, when the sole operand of the constructor is the proxied class.
The proxy
class being a private
member only matters when you explicitly name it. ADL can still find a function with a proxy
argument, which then indirectly causes access to a conversion function, because use of a class type isn't affected by access qualification. Only name lookup is affected. Likewise, you can always use a private
member type if you just have a typedef
to it.
这篇关于私人朋友操作符的代理类<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!