c++ - Declare a variable from the same type as an instance of that template -


i'm writing container template,

template <typename t, typename s> class container {     public:        s size(void) const { return mitems; }      private:        s mitems;        s mmaxitems;        t *marray; }; 

and in code this:

container<rooms, int> roomctr;  for(roomctr::type = 0; < roomctr.size(); i++) { } 

so variable matches s type without me specifying int hardcoded.

is possible?

the way found far this:

template <typename t, typename s> class container {     s type(void) const { return 0; }     ... };   for(decltype(table.type()) = 0; < roomctr.size(); i++) { } 

i wondering if there better way. or correct way it?

i'm using visual studio 2010.

use typedef in combination decltype:

// example program #include <iostream> #include <string> template <typename t, typename s> class container {     public:        typedef s sometype;        s size(void) const { return mitems; }      private:        s mitems;        s mmaxitems; };  struct {     //empty }; int main() {     container<a, int> roomctr;     decltype(roomctr)::sometype j = 0;        std::cout << j << std::endl; } 

Comments