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 the method instanceis in the template, I got the 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]':

If instance()located in Something, everything is in order.

Did I miss some syntactic subtleties? Or is this a template limitation?

+4
source share
1 answer

When outputting 'private' classes by default; otherwise, you must use the keyword "public" or "protected" for EVERY class from which you get:

class C : public A, public B 
{
     void f(); 
};
+3
source

Source: https://habr.com/ru/post/1661825/


All Articles