GCC allows access to private static member

This definitely looks like a bug since whether it's an instantiated template function or a real function should have no bearing on accessibility of private members in the base class. If you change your code to:

int bar(int&) {
    return PRIVATE;
}

then it rightly complains:

testprog.cpp: In member function 'int Derived::bar(int&)':
testprog.cpp:3:26: error: 'constexpr const int Base::PRIVATE' is private
     static constexpr int PRIVATE = 1;
                          ^
testprog.cpp:9:16: error: within this context
         return PRIVATE;
                ^

I would just raise this as a bug on gcc. If they do have a different view on its validity, they will let you know about it.

And, for when you do file the bug, I'd suggest using the absolute minimalist example that works, it'll make it much easier for them to debug. I got that down to:

class Base {
    static constexpr int PRIVATE = 42;
};
struct Derived : public Base {
    template <class T> int bar(T) {
        return PRIVATE;
    }
};
int main() {
    Derived d;
    return d.bar(1);
}

You may also want to indicate the various possibilities for declaring PRIVATE and their effect on gcc and clang (trunks as of this question):

                                gcc       clang
                              --------   --------
static constexpr int          accepted   rejected
static const int              accepted   rejected
const int                     rejected   rejected
int                           rejected   rejected

Using a non-templated function instead (as mentioned above):

int bar(int) {
    return PRIVATE;
}

seems to result in gcc "behaving" itself:

                                gcc       clang
                              --------   --------
static constexpr int          rejected   rejected
static const int              rejected   rejected
const int                     rejected   rejected
int                           rejected   rejected

So, if this is indeed a gcc problem, I'd be thinking that there's some interaction between static and templates which is causing the issue.

Tags:

C++

Gcc

Clang