此代码可以在 gcc 上编译,但不能在 clang 上编译。哪个编译器是正确的,如何让它在 clang-14 上编译,而不修改
A
的代码?
在 Ubuntu 22.04 上使用 clang v14.0.0。 它适用于 clang 上最新的不稳定版本,在 apt 存储库中不可用,我想修复此代码,而不必为此手动安装工具链,因为我预计不是每个人都会安装 trunk 版本。
#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);
}
来自 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 {
^
这是一个 clang bug 并且程序是格式良好。