与模板参数建立好友关系
不可能将模板参数添加为好友,因为标准不允许这样做。那么我怎样才能有效地得到同样的东西呢?
我想要的基本上是一种在拥有它的对象之外无法使用的类型。为什么不是重点,但如果您确实必须知道,我正在尝试制定一组智能指针来解决共享自有资源的问题。因此,我想要做的事情是这样的,如果它有效的话:
template < typename T, typename Owner >
struct accessible_member
{
private:
accessible_member() : val(T()) {}
accessible_member(T const& t) : val(t) {}
operator T& () { return val; }
operator T const& () const { return val; }
member_ptr<T> operator & () { return member_ptr<T>(val); }
friend class Owner;
};
因此,一个类不能将这个对象作为成员持有,除非它声明自己是所有者,并且如果它足够愚蠢地按原样公开它,那么它将是不可能在课堂外使用,因为太愚蠢了。
It's impossible to friend a template parameter because the standard disallows it. How might I get effectively the same thing then?
What I want is basically a type that is unusable outside the object which owns it. Why is rather beside the point but if you really must know, I'm trying to formulate a set of smart pointers that answer the problem of sharing an owned resource. Thus what I'm looking to do is something like so, if it worked:
template < typename T, typename Owner >
struct accessible_member
{
private:
accessible_member() : val(T()) {}
accessible_member(T const& t) : val(t) {}
operator T& () { return val; }
operator T const& () const { return val; }
member_ptr<T> operator & () { return member_ptr<T>(val); }
friend class Owner;
};
Thus a class can't hold this object as a member unless it declares itself the owner, and if it's silly enough to expose it as is, it will be impossible to use outside the class being so stupid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您对 C++98/03 的看法是正确的。但是,C++0x (n3225 11.4/3) 允许您使用以下语法执行此操作:
查看您的编译器是否允许您执行此操作。尝试打开 C++0x 支持。否则,解决方法会更丑陋:
...
然后根据您的编译器之一:
或:
You are correct about C++98/03. However C++0x (n3225 11.4/3) allows you to do this with this syntax:
See if your compiler will let you do that. Try turning on C++0x support. Otherwise the workarounds are uglier:
...
Then depending on your compiler one of:
or:
您可以使用它,然后让所有所有者继承 Owner。
然后,您可以使用 Owner 类来私有包装 accessible_member 中使用的方法。
所有者现在可以访问accessible_member。 Friend 不是继承的,因此您可以提供(包装)必要的方法,以便所有继承 Owner 的类都可以使用 accessible_member。
这是一个 2 级解决方案,但它保持了封装级别。
然后,您可以使用受保护的方法在从 Owner 继承的结构中间接使用accessible_member:
查看最后一个示例 模板好友。
You could use this, and then let all the owners inherit from Owner.
You could then use Owner class to wrap privately the methods used in accessible_member.
accessible_member is now accessible to Owner. Friend is not inherited, so you can supply (wrap) the necessary methods so all the classes that inherit Owner can use accessible_member.
It's a 2 level solution but it keeps the level of encapsulation.
Then you can use the accessible_member indirectly in structs that inherit from Owner using the protected methods:
Look at the last example at Template Friends.
7.1.5.3 p2 说:
因此,任何允许您以任何方式实现的解决方案都将不符合标准。
7.1.5.3 p2 says:
As a result, any solution that allows you to it by any mean will be non standard conformant.
简单地定义一个从accessible_member继承的私有嵌套类型怎么样?当然
,这仍然意味着原始
accessible_member
类型可供任何人使用,因此考虑起来可能没有多大用处。What about simply defining a private nested type that trivially inherits from accessible_member? Something like
Of course that still means the original
accessible_member
type is available to anyone so it's maybe not of much use come to think of it.我能看到的唯一解决方法相当丑陋,并且使用 CRTP:
The only workaround I can see is rather ugly and uses the CRTP:
据我所知,在 C++98 下只要稍加改变就可以了。它编译时没有对我发出任何警告
g++-4.1 -Wall -Wextra -pedantic -ansi -std=c++98
只需更改
为
(我在 Stackoverflow 上得到了答案,这个问题已经出现了几次:模板参数作为朋友)
That will work, with a little change, under C++98 as far as I can see. It compiled without any warnings for me with
g++-4.1 -Wall -Wextra -pedantic -ansi -std=c++98
Just change
to
(I got that answer on Stackoverflow, this question has come up a few times: Template parameter as a friend )