这是一个在没有警告的情况下编译的程序,例如GNU C++:
$ g++ -o t -Wall -pedantic -Wshadow t.cpp
$ ./t.exe
Calling barney::barney()
Calling foo::operator()()
Calling barney::barney()
Run Code Online (Sandbox Code Playgroud)
但它完全无法在MSVC++上编译:
$ cl /EHsc t.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
t.cpp
t.cpp(17) : error C2380: type(s) preceding 'fred' (constructor with return type, or illegal redefinition of current class-name?)
t.cpp(17) : error C2208: 'fred' : no members defined using this type
Run Code Online (Sandbox Code Playgroud)
更重要的是,当它编译时,输出不是我所期望的.有人可以了解这段代码所需的标准行为吗?
这里是:
#include <iostream>
using ::std::cerr;
struct fred;
struct …Run Code Online (Sandbox Code Playgroud) c++ ×1