更新时间:2022-04-10 05:44:23
如果使用 / Za选项禁用语言扩展,编译器拒绝这两个调用:
If you compile with the /Za option to disable language extensions, the compiler rejects both calls:
> cl /Za test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(11): error C2664: 'void f2(char &)' : cannot convert argument 1 from 'char' to 'char &'
test.cpp(12): error C2664: 'void f4(A &)' : cannot convert argument 1 from 'A' to 'A &'
A non-const reference may only be bound to an lvalue
有几种(非常受限制的)编译器在启用语言扩展的情况下,将仍然允许非const常量引用绑定到右值表达式。我的理解是,这主要是为了避免破坏依赖这个扩展的几个巨大的遗留代码库。
There are several (very constrained) circumstances in which the compiler, with language extensions enabled, will still allow a non-const lvalue reference to bind to an rvalue expression. My understanding is that this is largely to avoid breaking several enormous legacy codebases that rely on this "extension."
(通常,不推荐使用/ Za原因,但主要是因为Windows SDK标头不能包含/ Za选项。)
(In general, use of /Za is not recommended for many reasons, but mostly because the Windows SDK headers cannot be #included with the /Za option.)