C++17 编译器(gcc 或 Microsoft Visual C++)是否有禁止该功能的选项 "not produce a temporary"

The C++17 compiler (gcc or Microsoft Visual C++), does it have an option that prohibit the feature "not produce a temporary"

在以下情况下,我如何告诉 С++17 编译器创建临时文件 (即 C++17 编译器必须考虑 copy/move 操作,就像 C++11 和 C++14 编译器一样)

class A{
    public:
        A(){}
        A(const A&)=delete;
        A(A&&)=delete;
};
A f(){
    return A();
}
int main(){
    auto a=f();
}

输出(c++14 - 这就是我想要的):

gcc -std=c++14 ./a.cpp
./a.cpp: In function ‘A f()’:
./a.cpp:8:11: error: use of deleted function ‘A::A(A&&)’
  return A();
           ^
./a.cpp:5:3: note: declared here
   A(A&&)=delete;
   ^
./a.cpp: In function ‘int main()’:
./a.cpp:11:11: error: use of deleted function ‘A::A(A&&)’
  auto a=f();
           ^
./a.cpp:5:3: note: declared here
   A(A&&)=delete;
   ^

输出(c++17):

gcc -std=c++17 ./a.cpp
./a.out

(successfully run)

我的客户拥有大量 C++11 代码和 C++17 编译器:gcc 8.3.0 和 Microsoft C++ 编译器(来自 Visual Studio 2017 和 2019)。 并且有很多地方包含上面的代码。 C++17 编译器,在这种情况下是否有禁止“不产生临时文件”功能的选项?

您不能(也不应该)停用保证省略。但是,如果你想在特定情况下防止它,你需要做的就是不使用纯右值:

A f(){
    A a{};
    return a;
}

这将需要 A 可以移动,尽管移动仍然可以(并且几乎肯定会)被省略。

保证省略的要点之一是您现在可以 return 不可移动类型的纯右值。这允许您通过工厂函数进行更复杂的初始化。这不是您应该想要在全球范围内阻止的事情。