0

I am attempting to template a stack class and in the implementation of the class in the seperate file there is this line.

typedef [namespace]::stack<double> number_stack;

I am attempting to template this so that it will accept the generic T type.

The end goal then, would be to make it such that the templated type would allow for complex numbers as entries on the stack.

Could anyone help me with this issue please?

cdhowie
  • 158,093
  • 24
  • 286
  • 300
user43792
  • 13
  • 3
  • Do you mean `typedef N::stack number_stack;` ? If not then please clarify your question – M.M Sep 12 '14 at 04:37

1 Answers1

2

If you want number_stack to typedef a specific instantiation of template stack, just go

typedef stack<int> number_stack;

If you want the typedef number_stack itself "to be templated", you need c++11 type alias

template < typename T > class stack {};

template < typename T > using number_stack = stack < T >;

typedef number_stack<int> int_stack;

This use of using is basically making the standard typedef templated.

Slyps
  • 607
  • 4
  • 9
  • And, just for completeness, the C++ < 11 way of doing this is `template struct number_stack { typedef stack type; };` which you would use like `typename number_stack::type an_int_stack;` – cdhowie Sep 12 '14 at 05:05
  • @cdhowie You don't need that `typename` there for `an_int_stack`. – T.C. Sep 12 '14 at 05:50
  • @T.C. You are right, I was running low on caffeine. :) – cdhowie Sep 12 '14 at 14:54