![]() |
|
Can I adress a static const member in a template without a template argument? |
weapon_S
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. |
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. [EDIT] Actually, don't do the void thing. It doesn't work. |
J-Gamer
Member #12,491
January 2011
![]() |
Could you try having a class on top of it? class MasterParent { public: static const ALLEGRO_EVENT_TYPE STATIC_MEMBER; } 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 |
weapon_S
Member #7,859
October 2006
![]() |
Hahahahaha 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. //(1 template Master::STATIC_MEMBER; //(2 Master<...>::STATIC_MEMBER;
References
|
|