$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
a.cc
$ cat a.cc
#include<iostream>
using namespace std;
static int x = 5053;
void f2();
int main() {
cout << "a: " << x << endl;
f2();
return 0;
}
b.cc
$ cat b.cc
#include<iostream>
using namespace std;
static int x = 4921;
string f2() {
cout << "b: " << x << endl;
return "";
}
./a.out
$ g++ --std=c++17 a.cc b.cc && ./a.out
a: 5053
b: 4921
Why was I able to forward declare string f2();
from b.cc
as void f2();
in a.cc
?
Any references to cppreference or spec that allows this would be appreciated.
15
This seems to be a apple clang bug as the call f2()
in main chooses the void f2()
version which is undefined(not implemented) and so we should get a linker error.
We do get a linker error with both clang and gcc. Demo. So this seems to be a apple clang specific bug.