Is copy/move elision allowed when returning *&object?

Viewed 374

Look at this code:

#include <stdio.h>

struct Foo {
    Foo() { }
    Foo(const Foo &) { printf("copy\n"); }
    Foo(Foo &&) { printf("move\n"); }
};

Foo getFoo() {
    Foo f;
    return *&f;
}

int main() {
    getFoo();
}

C++14 Standard says (12.8/31) that copy/move elision allowed:

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv- unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

In my example, return expression is not a name, so I don't think elision is allowed.

I've checked GCC/clang/MSVC, and while clang/MSVC doesn't elide copy, GCC does. Does GCC violate the standard here?

1 Answers
Related