C++14/1y: Standard ref for "operator+ must take either one or two arguments"?

StackOverflow https://stackoverflow.com/questions/23286828

  •  09-07-2023
  •  | 
  •  

質問

Where in the C++1y/C++14 standard does it say that the following translation unit is ill-formed?

struct S {};

void operator+(S,S,S);

The error is:

error: ‘void operator+(S, S, S)’ must take either one or two arguments
役に立ちましたか?

解決

[over.unary]

1 A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters or a non-member function with one parameter. Thus, for any prefix unary operator @, @x can be interpreted as either x.operator@() or operator@(x). If both forms of the operator function have been declared, the rules in 13.3.1.2 determine which, if any, interpretation is used. See 13.5.7 for an explanation of the postfix unary operators ++ and --.

[over.binary]

1 A binary operator shall be implemented either by a non-static member function (9.3) with one parameter or by a non-member function with two parameters. Thus, for any binary operator @, x@y can be interpreted as either x.operator@(y) or operator@(x,y). If both forms of the operator function have been declared, the rules in 13.3.1.2 determine which, if any, interpretation is used.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top