2

次のコードを見てください。

struct s
{
    s&  operator+() {return*this;}
};

void foo(s &)   {}

s bar() {}

int main()
{
    foo(bar());     //comp error
    foo(+bar());    //ok
}

コンパイラは operator+() を最適化するので、main() の 2 行は同等であると思いますよね? 一方を受け入れて他方を受け入れないことに、どのような意味があるでしょうか。

4

1 に答える 1

6

The line foo(bar()) is trying to bind an rvalue of type s (the temporary returned by bar()) to a non-const lvalue reference to s (the argument of foo()). This is illegal, hence the compilation error. rvalues can only be bound to rvalue references or to const lvalue references.

The expression +bar(), on the other hand, returns an lvalue reference (that's the return type of operator + ()), which can be bound to the lvalue reference parameter of foo().

Beware though: you are returning an lvalue reference to a temporary here. While using it inside foo() is still safe (the temporary will be destroyed when the full-expression in which it is created is completely evaluated, so after foo() returns), you would get Undefined Behavior if you stored that reference somewhere and dereferenced it later.

Therefore, instead of regarding this as a "solution to a compilation error", you should consider it a way to blindfold the compiler so you are free to sneak into big troubles.

于 2013-02-16T10:35:53.573 に答える