- Online Community Forums » Programming Questions » Can I adress a static const member in a template without a template argument?

This thread is locked; no one can reply to it. rss feed Print
Can I adress a static const member in a template without a template argument?
Member #7,859
October 2006

I have something like this:

1template<typename T> 2class Master{ 3 public: 4 static const ALLEGRO_EVENT_TYPE STATIC_MEMBER; //It really is an unsigned int 5}

1class A; 2Master<A> ma;

1#include "master.hpp" 2class B{ 3 void check_event(ALLEGRO_EVENT_TYPE x){ 4 if(x==Master::STATIC_MEMBER) 5 zomfg(); 6 else 7 rofl(); 8 } 9};

Including the header for class A, just to type "Master<A>::STATIC_MEMBER" seems messy, and saying "Master<any type, because the static member doesn't depend on it>::STATIC_MEBER" seems wrong.
Is there a way around this... most preferably pre C++11. Thanks in advance.

Stas B.
Member #9,615
March 2008

I don't think so. I guess you could do Master<void>::STATIC_MEMBER or something. The meaning is clear and there are no practical implications. Arguably, if you need to use that constant in places that don't need the rest of the class, it shouldn't be a part of it at all. The compiler will copy your entire template class into helper.hpp just for that single constant and that's not exactly optimal.


Actually, don't do the void thing. It doesn't work. :-/

Member #12,491
January 2011

Could you try having a class on top of it?

class MasterParent {

template<typename T>
class Master<T> : public MasterParent {
   //rest of your class that does use the template

(this is just a guess...)

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Member #7,859
October 2006

Hahahahaha :-[
Actually class A and B were the same one, only in my head were they separated things.(Because in this case the class actually does two things). That solves it ;D
Also, I've found this method that gave no errors[1]

1#include "master.hpp" 2 3//Forward declaration 4class unknown; 5 6void check_event(ALLEGRO_EVENT_TYPE x){ 7 if(x==Master<unknown>::STATIC_MEMBER) //Compiler needs no further information to resolve this 8 zomfg(); 9 else 10 rofl(); 11}

J-Gamer, I have considered that option, but I would consider that more of a work-around. Luckily I've found this.
Stas, and J-Gamer thanks for trying. Here's some of my fails, for laughs: ;D

template Master::STATIC_MEMBER;


  1. I haven't done linking yet, but I believe this is one of those cases for which C++'s advanced name mangling was invented. And obviously you need to instantiate the template /somewhere/.
Go to: