GLmonster GLmonster - 1 year ago 59
C++ Question

Propagate method staticness to derived class

template<typename T> struct Derived: T
/*static*/ int foo(int x) { return T::foo(x) + 1; }

T::foo(int x)
Derived<T>::foo(int x)
should also be
. Otherwise
Derived<T>::foo(int x)
should be non-static.

Is there a way to let the compiler take care of this?

Answer Source

No, you cannot propagate staticness in the sense you ask. Incidentally, you could also ask the same thing about const:

int foo(int x) { return bar(x) + 1; } // Infer that foo is const because bar is

C++ specifiers are meant to convey intent about the interface, on which users can rely even if the implementation changes:

static int foo(x); // This method does not require an object
int foo(x) const; // This method will not modify the object

In case - through templates, for example - the implementation may vary, your interface must reflect the lowest common denominator. For const, for example, methods need to be non-const. For static, which is your question, you cannot declare static.

Note that this is not a huge imposition. Even if a method is static, you can still call it using with object semantics. So, in your case, you'll have to just use object semantics. In particular, regarding your clarification in the comments

If allocator is static then container doesn't need to hold it's pointer. So decorators must preserve staticness.

note that decorators can also not preserve staticness, because containers can hold pointers in any case, and call them via object notation, regardless of their constness.