This code compiles on gcc but not on clang. Which compiler is correct, and how to make it compile on clang-14, without modifying A
‘s code?
Using clang v14.0.0 on Ubuntu 22.04.
It works on the latest unstable version on clang, not available in apt repos, and I would like to fix this code without having to install manually the toolchains just for this, since I expect not everybody will have the trunk version installed.
#include <iostream>
#include <cstdint>
template<typename T>
class B
{
};
class A
{
struct Sub {
int r;
};
template<typename>
friend class B;
};
template<>
struct B<int>
{
template<typename T>
using Sub = typename T::Sub;
};
int main()
{
int i = offsetof(B<int>::Sub<A>, r);
}
error from clang 14.0.0:
<source>:23:5: error: 'Sub' is a private member of 'A'
using Sub = typename T::Sub;
^
<source>:11:12: note: implicitly declared private here
struct Sub {
^
10
Which compiler is correct
This is a fixed clang bug and the program is well-formed.
Note that clang trunk starts accepting the program. Demo
Here is the clang bug report:
Clang rejects valid friend access