4

私は次のコードをいじっていましたが、Visual Studio 2017 アプリケーションと 2 つの異なるオンライン コンパイラを使用して異なる結果を得ました。リリース モードでは、ビジュアル スタジオはどちらの場合もコピー/移動を省略しますが、2 つのオンライン コンパイラは、括弧で囲まれていない return ステートメントの場合にのみそれを行います。私の質問は、誰が正しいかということです。さらに重要なのは、その根底にあるルールは何かということです。(括弧を構文と組み合わせて使用​​できることは知っていますdecltype(auto)が、これは現在のユースケースではありません)。

コード例:

#include <iostream>
#include <cstdio>

struct Foo
{
    Foo() { std::cout << "default constructor" << std::endl; }
    Foo(const Foo& rhs) { std::cout << "copy constructor" << std::endl; }
    Foo(Foo&& rhs) { std::cout << "move constructor" << std::endl; }
    Foo& operator=(const Foo& rhs) { std::cout << "copy assignment" << std::endl; return *this; }
    Foo& operator=(Foo&& rhs) { std::cout << "move assignment" << std::endl; return *this; }
};

Foo foo_normal()
{
    Foo a{};
    return a;
}

Foo foo_parentheses()
{
    Foo a{};
    return (a);
}

int main()
{
    auto a = foo_normal();
    auto b = foo_parentheses();
    std::getchar();
}

オンライン コンパイラ 1: http://cpp.sh/75bux

オンライン コンパイラ 2: http://coliru.stacked-crooked.com/a/c266852b9e1712f3

リリース モードでの Visual Studio の出力は次のとおりです。

default constructor
default constructor

他の 2 つのコンパイラでは、出力は次のようになります。

default constructor
default constructor
move constructor
4

2 に答える 2