简体   繁体   中英

How to compile this both in clang and gcc?

The below code compiles in g++ but error out in clang++. How to fix this code so that it can compile in both?

class A {
    private:
        template<int _N> class B {
            public:
                static constexpr int N = _N;
        };
        class C {
            public:
                template <typename B>
                B* func();
        };
};

template <typename B>
B* A::C::func()
{
    constexpr int N = B::N;
}

clang prior clang 11 is unbale to resolve the name B properly. The solution is use another template parameter name:

template <typename D>
D* A::C::func()
{
    constexpr int N = D::N;
}

The full example requires the public access to A::C at least or a friend function.

class A {
    private:
        template<int N_> class B {
            public:
                static constexpr int N = N_;
        };
        class C {
            public:
                template <typename B>
                B* func();
        };
    friend int main();
};

template <typename D>
D* A::C::func() {
    constexpr int N = D::N;
    return nullptr;
}

int main() {
    A::C c;
    auto *n = c.func<A::B<0>>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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