如何提升运营商的工作? [英] How do boost operators work?
问题描述
的boost ::运营商
自动基于手动实现比如 +定义操作符,如
这是非常有用的。要生成 +
= T
的经营者,一个来自继承的boost ::符< T>
如由升压例如:
boost::operators
automatically defines operators like +
based on manual implementations like +=
which is very useful. To generate those operators for T
, one inherits from boost::operators<T>
as shown by the boost example:
类敏:升压::经营者&LT;&敏GT;
我熟悉的CRTP模式,但我看不出它是如何工作在这里。具体来说,我真的没有任何继承自设施的运营商不成员。 的boost ::运营商
似乎是完全空的,但我不是很擅长阅读提振源$ C $ C。
I am familiar with the CRTP pattern, but I fail to see how it works here. Specifically, I am not really inheriting any facilities since the operators aren't members. boost::operators
seems to be completely empty, but I'm not very good at reading boost source code.
谁能解释这是如何工作的细节?是这种机制公知和广泛使用的
Could anyone explain how this works in detail? Is this mechanism well-known and widely used?
推荐答案
有一个大的多重继承链,其中在顶部有一些实现经营类,但这样做朋友
的功能,从而将它们放置在封闭的空间,而不是作为类的成员。
There's a big multiple inheritance chain, at the top of which there are a number of classes that implement the operators, but do so as friend
functions, thus placing them in the enclosing namespace rather than as members of the class.
例如,最终实施运营商+
变成了:
For example, the final implementation of operator+
becomes:
template <class T, class U, class B = ::boost::detail::empty_base<T> >
struct addable2 : B
{
friend T operator +( T lhs, const U& rhs ) { return lhs += rhs; }
friend T operator +( const U& lhs, T rhs ) { return rhs += lhs; }
};
这篇关于如何提升运营商的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!