gpt4 book ai didi

c++ - 尝试解决 gcc 特定的编译错误

转载 作者:行者123 更新时间:2023-12-02 02:26:07 25 4
gpt4 key购买 nike

这是我正在尝试编译的程序。

template <typename T>
struct A
{
T foo() const
{
return 1;
}
};

template <typename T>
struct B : A<T>
{
using U = A<T>;
T foo() const
{
return 2;
}
};

template <typename T>
struct D
{
B<T> b;
int foo() const
{
// using U = typename B<T>::U; // compilation succeeds if I uncomment this line
return b.U::foo();
}
};

int main()
{
D<int> d;
return d.foo();
}

我发现 gcc 9.2、clang 9.0.0 和 icc19.0.1 的行为有所不同。该程序无法使用 gcc 进行编译,并给出错误 U has not beelated,使用 clang 可以正常编译,并且使用 icc 进行了不平凡的汇编。为什么这些编译器的行为不同?

我尝试阅读dependent_name但由于我无法正确遵循它而放弃了。据我所知,结构 D 内的数据成员 b 具有依赖类型,并且 U 在实例化模板时应该已经解析。

使用 godbolt.org 上的上述编译器编译了上述代码:https://godbolt.org/z/n_Wl44 。我使用带有 -O3 标志的 C++17。提前致谢!!!

最佳答案

Ub.U::foo();应该在实例化期间在 B<T> 的上下文中查找首先,找到using U = A<T>; ,这样 b.U::foo()来电 foo()定义于 A<T>[basic.lookup.classref]/4

查找应延迟到实例化为止,并且不应进行定义查找,因为 b是依赖的,因为它的类型取决于模板参数 T[temp.dep.expr]/5

因此这似乎是 GCC 中的一个错误。 this bug report 中发布了一个非常相似的代码示例。 ,据我所知,这表明了同样的问题。该错误当前标记为"new"状态。

据我所知,the godbolt.org conformance view posted by @TedLyngmo 中显示的其他失败编译器是 GCC 的衍生物,因此独立编译器对此并不存在分歧。

关于c++ - 尝试解决 gcc 特定的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com