0

I have

template <typename A, typename B, typename C> class Template { public: static const size_t ZONE_X = 0; static const size_t ZONE_Y = 1; ... } 

What is the most elegant way I to access the static const variables from other templates that in my case are dependency injection or policy to this one? ... or I should just define the constants out of the template?

2
  • you can access by Template::Zone_X in other templates Commented Mar 12, 2014 at 15:57
  • in msvc-10 it reports error - error C2955: 'Template' : use of class template requires template argument list Commented Mar 12, 2014 at 16:26

2 Answers 2

1

You can use

Template<void,void,void>::ZONE_X 

Note that, the three voids are needed for Template given its definition. Of course, you can use other types, e.g. int or mixed of them:

Template<int,int,int>::ZONE_X 

or

Template<void,int,float>::ZONE_X 
Sign up to request clarification or add additional context in comments.

6 Comments

And if Template has a data member A m_data;?
@PetrBudnik If it is static, the answer should also work. If it's not, you need to create a object, e.g. Template<void,void,void> obj, and then use obj.m_data.
I guess, I was not clear. If given Template in the rest of the code (marked ...) has a member A m;, you cannot simply instantiate it with void. It simply won't compile, unless you have a specialization... Anyway, I think OP has a design problem - one should not be needing to instantiate template just to get a static data member...
@PetrBudnik Yes, the types are depended on how OP uses them. The answer will work given little info of Template. :)
@gsf Really? I don't know this. Why does gcc have such constraint?
|
0

the argument list part should be taken with to refer the static member maybe you should not defined it in a template

#include <iostream> using namespace std; template <typename A, typename B, typename C> class Template { public: static const size_t ZONE_X = 0; static const size_t JOIN_Y = 1; }; template<typename A> class Template2 { public: static size_t get_zone_x() { return Template<A,A,A>::ZONE_X; } }; int main() { std::cout << Template<int,int,int>::ZONE_X << std::endl; std::cout << Template2<int>::get_zone_x() << std::endl; return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.