eddie kuo
eddie kuo

Reputation: 728

Can names in unnamed namespaces in different C++ files refer to the same named thing?

Clang and GCC have a difference of opinion in this problem:

With g++ "result2" is printed, but with clang++ it's "result1".

I know g++ think the A thrown by g(), is not the same A is the main.cpp.
But, is there has anything wrong with clang++?

version:
g++: 7.4.0
clang: 10.0.0

main.cpp:

#include <iostream>

namespace {
  struct A {};
}

extern void g();

int main()
{
  try {
    try {
      g();
    } catch (A) {std::cout << "result1\n";}
  } catch (...) {std::cout << "result2\n";}
}

other.cpp:

namespace {
  struct A {};
}

void g() { throw A(); }

Upvotes: 3

Views: 115

Answers (1)

Davis Herring
Davis Herring

Reputation: 39868

The names A have internal linkage here; they can’t possibly refer to the same thing in different translation units. GCC is correct in this case; some implementations use names to implement RTTI, which may be at fault here.

Upvotes: 6

Related Questions