【发布时间】:2019-06-04 15:51:24
【问题描述】:
以下代码在 clang 上编译并运行,但在 gcc 上失败并显示“错误:非静态数据成员‘Outer::a’的无效使用”:
#include <functional>
#include <vector>
#include <assert.h>
#include <iostream>
#include <memory>
class Outer
{
public:
bool a = false;
virtual void f() = 0;
template <typename T>
class Inner : public T
{
public:
virtual void f() override
{
a = true; // Note: accessed through inheritance, not through outer scope
}
};
};
struct Foo : Outer { };
int main()
{
Outer::Inner<Foo> f;
f.f();
}
将“this->a”添加到内部类使其适用于两个编译器,但我想知道正确的行为是什么以及标准对此有何规定。
有趣的是,上面的代码可以作为 VS2017 中更大代码库的一部分在工作中使用,但是当我在家里单独尝试使用 VS2017 时,它会失败,并出现与 GCC 相同的错误。
你可以尝试在这里编译:
【问题讨论】:
-
如果您删除
main()和bool a数据成员中的代码,则代码会在 clang 中失败(如果它是一致的,则不应该这样做)。所以我认为这是clang中的一个错误。 -
也许用
language-lawyer标签替换其中一个标签 -
应该是 CLang 的错误。我不明白它怎么可能不是编译错误。
-
@NathanOliver 啊,我不是第一次意识到这一点。似乎 gcc 是正确的,那么问题是为什么 clang 允许它。您链接中的示例没有。
标签: c++ gcc clang language-lawyer