gsf
gsf

Reputation: 7232

access constant declared in a c++ template

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?

Upvotes: 0

Views: 86

Answers (2)

herohuyongtao
herohuyongtao

Reputation: 50667

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

Upvotes: 1

michaeltang
michaeltang

Reputation: 2898

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;
}

Upvotes: 0

Related Questions