Sébastien Le Ray Sébastien Le Ray - 8 days ago 8
C++ Question

C++ nested templates: inaccessible static method

I have a strange (or maybe not) error with the following code:

template<typename T>
class Registrer {
public:
Registrer() {
Registry::register(T::instance);
}
};

template<typename T>
class RegisteringClass {
private:
static const Registrer<T> REGISTRER;
public:
RegisteringClass () { Q_UNUSED(REGISTRER); /* force static instantiation */ }
static const WhatEver *instance() { static T INSTANCE; return &INSTANCE; }
};

template<typename T>
const Registrer<T> RegisteringClass<T>::REGISTRER;

class Something : public WhatEver, RegistringClass<Something> {
}


When
instance
method is in the template, I got following error (gcc)

error: 'static const WhatEver* RegisteringClass<T>::instance() [with T = Something]' is inaccessible
static const WhatEver*instance() { static T INSTANCE; return &INSTANCE; }
error: within this context
Registry::register(T::instance);
In instantiation of 'static const WhatEver* RegisteringClass<T>::instance() [with T = Something]':


Whereas when
instance()
is in
Something
, all is fine.

Did I miss some syntax subtlety? Or is this a template limitation?

Answer

When deriving classes, 'private' is default; otherwise you have to use 'public' or 'protected' keyword for EVERY class you derive from:

class C : public A, public B 
{
     void f(); 
};
Comments