是免费的operator-> *重载的邪恶? [英] Are free operator->* overloads evil?
问题描述
在驳斥观点
I was perusing section 13.5 after refuting the notion that built-in operators do not participate in overload resolution, and noticed that there is no section on operator->*
. It is just a generic binary operator.
它的兄弟, operator->
,运算符*
和 operator []
都需要是非静态成员函数。这排除了对用于从对象获得引用的操作符 的自由函数重载的定义。但是不包括不常见的 operator-> *
。
Its brethren, operator->
, operator*
, and operator[]
, are all required to be non-static member functions. This precludes definition of a free function overload to an operator commonly used to obtain a reference from an object. But the uncommon operator->*
is left out.
特别是 []
有很多相似之处。它是二进制的(他们错过了一个黄金的机会使它成为n元),它接受左边的某种容器和右边的某种定位器。它的特别规则一节,13.5.5,似乎没有任何实际效果,除了非法自由功能。 (这种限制甚至排除了对交换性的支持!)
In particular, operator[]
has many similarities. It is binary (they missed a golden opportunity to make it n-ary), and it accepts some kind of container on the left and some kind of locator on the right. Its special-rules section, 13.5.5, doesn't seem to have any actual effect except to outlaw free functions. (And that restriction even precludes support for commutativity!)
因此,例如,这是完美法律:
#include <utility>
#include <iostream>
using namespace std;
template< class T >
T &
operator->*( pair<T,T> &l, bool r )
{ return r? l.second : l.first; }
template< class T >
T & operator->*( bool l, pair<T,T> &r ) { return r->*l; }
int main() {
pair<int, int> y( 5, 6 );
y->*(0) = 7;
y->*0->*y = 8; // evaluates to 7->*y = y.second
cerr << y.first << " " << y.second << endl;
}
很容易找到用途,但是替代语法不会那么糟糕。例如,向量的缩放索引
:
It's easy to find uses, but alternative syntax tends not to be that bad. For example, scaled indexes for vector
:
v->*matrix_width[2][5] = x; // ->* not hopelessly out of place
my_indexer<2> m( v, dim ); // my_indexer being the type of (v->*width)
m[2][5] = x; // it is probably more practical to slice just once
标准委员会忘了防止这种情况,
Did the standards committee forget to prevent this, was it considered too ugly to bother, or are there real-world use cases?
推荐答案
Googling一点,我发现更多的实例的人询问是否使用 operator-> *
比实际建议。
Googling around a bit, I found more instances of people asking whether operator->*
is ever used than actual suggestions.
几个地方建议 T& A :: operator-> *(TB :: *)
。不确定这是否反映了设计师的意图或者错误地描述 T& A :: operator-> *(T A :: *)
是内置函数。不是真的与我的问题有关,而是给出了我在在线讨论&文学。
A couple places suggest T &A::operator->*( T B::* )
. Not sure whether this reflects designer's intent or a misimpression that T &A::operator->*( T A::* )
is a builtin. Not really related to my question, but gives an idea of the depth I found in online discussion & literature.
有一个提到D& E 11.5.4,我想是C ++的设计和进化。也许这包含一个提示。否则,我只是得出结论,这是一个无用的丑陋,被标准化和大多数其他人忽略了。
There was a mention of "D&E 11.5.4" which I suppose is Design and Evolution of C++. Perhaps that contains a hint. Otherwise, I'm just gonna conclude it's a bit of useless ugliness that was overlooked by standardization, and most everyone else too.
编辑
为了定量化, - > *
是D&最紧密的绑定操作符,可以通过自由函数重载。所有后缀表达式和一元运算符重载需要非静态成员函数签名。一元运算符之后的优先级是C风格的转型,这可以说是对应于转换函数( operator type()
),也不能是自由函数。然后是 - > *
,然后乘法。 - > *
可能像 []
或像%
,他们可以采取任何一种方式,他们选择 EEEEEEVIL 的路径。
To put this quantitatively, ->*
is the tightest binding operator that can be overloaded by a free function. All the postfix-expression and unary operators overloads require nonstatic member function signatures. Next precedence after unary operators are C-style casts, which could be said to correspond to conversion functions (operator type()
), which also cannot be free functions. Then comes ->*
, then multiplication. ->*
could have been like []
or like %
, they could have gone either way, and they chose the path of EEEEEEVIL.
这篇关于是免费的operator-> *重载的邪恶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!