簡體   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