开发者

unary minus overload: member or non-member?

Given that prefix unary operators can be "implemented by a non-static member function with no parameters or a non-member function with one parameter" (§13.5.1[over.unary]/1), is there a difference besides the usual enca开发者_Go百科psulation/code reuse design rationales that apply to any member/non-member function choices?

For binary operators, there's a semantic difference because non-members allow implicit conversions of their left-hand operands. There doesn't seem to be anything like that for the unary operators, yet the standard defines std::complex's unary negation operator as a non-member (§26.4.6[complex.ops]), while std::valarray's and std::duration's unary negation operators are members (§26.6.2.6[valarray.unary], §20.11.5.3[time.duration.arithmetic]). Is there a nuance?


As far as I'm aware there are no differences as compared to deciding if a non-operator function should be member or non-member. Obviously prefer non-member, non-friend when possible (like the standard algorithms).


Using members when possible makes more sense, since you don't have to go crazy with friends. But otherwise, it is just a code style decision.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜