Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedefing non-type template parameter

Tags:

c++

templates

I would like to access a template parameter outside of a class. I usually do this as follows:

template <class T>
class A
{
  typedef typename T T;
}

A<int>::T;

I would like to be able to do the same for non-type template parameters. This doesn't work:

template <int T>
class A
{
  typedef typename T T;
}

A<3>::T;

I will clarify why I need this. I want to define a second class as follows:

template <class C>
class B
{
  static int func() {return C::T;}
}

B<A<3> >::func();

What is the correct way to do this? Thank you very much.

like image 942
Benjy Kessler Avatar asked Feb 18 '23 13:02

Benjy Kessler


1 Answers

That's because T is not a type name and you cannot typedef it. It is an int value and, if you want to access it as a static member of the class, you need a static member int. Seems like what you really want is this:

template <int T>
class A
{
  public:
    static const int x = T;
};

doSomething(A<5>::x);
like image 151
Joseph Mansfield Avatar answered Feb 20 '23 02:02

Joseph Mansfield