繁体   English   中英

使用别名作为 scope 获取父成员时的不同编译器行为

[英]Different compiler behaviour when using alias as scope to get parent member

此代码在 Clang 和 Visual C++ 上编译良好,但在 GCC 上编译良好:

#include <iostream>


template <class T>
struct Test {
    Test(T &t) : _t(t) {
    }
    
    void method() {
        std::cout << _t.Internal::_value << "\n";       // Doesn't work on GCC
        std::cout << _t.T::Internal::_value << "\n";    // Work on all compilers
    }

private:
    T &_t;
};

template <class T>
struct Base {
    T _value = 1;
};

template <class T>
struct Child : Base<int> {
    using Internal = Base<int>;
    
    int _value = 2;
};

int main(int argc, const char * argv[]) {
    Child<float> child;
    Test<Child<float>> test(child);
    
    test.method();
    
    return 0;
}

来自 GCC 的错误消息是

error: 'Internal' has not been declared
    9 |         std::cout << _t.Internal::_value << "\n";
      |                         ^~~~~~~~

哪一个是对的?

Visual C++ 和 Clang 正确接受此代码。

这是 GCC 中的一个错误,它阻止了它做同样的事情。 问题中的错误高达 GCC 10,在 GCC 11 中,其措辞更改为

error: request for member 'Internal' in non-class type 'T'
    9 |         std::cout << _t.Internal::_value << "\n";
      |                         ^~~~~~~~

并且 GCC 中继最终也接受了代码。 演示: https://gcc.godbolt.org/z/dj34Yhns3

所以我们可以期待 GCC 12 中的修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM