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

I'm writing a 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 the code I would like to do this:

Container<Rooms, int> RoomCtr;

for(RoomCtr::type i = 0; i < RoomCtr.size(); i++)
{
}

so that the variable matches the S type without me specifying the int hardcoded.

Is this possible?

The only way I found so far is this:

template <typename T, typename S>
class Container
{
    S type(void) const { return 0; }
    ...
};


for(decltype(Table.type()) i = 0; i < RoomCtr.size(); i++)
{
}

I was wondering if there is a better way. Or would this be the correct way to do it?

I'm using currently Visual Studio 2010.

Answers


Use typedef in combination with 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 A
{
    //empty
};
int main()
{
    Container<A, int> roomCtr;
    decltype(roomCtr)::SomeType j = 0;   
    std::cout << j << std::endl;
}

You can do a simple typedef:

template <typename T, typename S>
class Container {
  T *data;
  /* other variables */

  public:
    typedef S size_type;
    Container();

};

int main() {
  Container<char, int>::size_type something;
}

Here something will be of type int.


You could use typedefs to solve your problem. E.g.:

// Example program
#include <iostream>
#include <string>


template <typename T, typename S>
class Container
{
    public:
       S size(void) const { return mItems; }

    private:
       S mItems;
       S mMaxItems;
};

struct A
{
    //empty
};
int main()
{
    typedef int SomeType;
    Container<A, SomeType> RoomCtr;

    for(SomeType i = 0; i < 10; i++)
        std::cout << i << std::endl;

}

You could also explicitly keep the type in the class with a typedef. E.g.:

// 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 A
{
    //empty
};
int main()
{
    typedef int MyType;
    Container<A, MyType> roomCtr;
    Container<A, MyType>::SomeType j = 0;   
    std::cout << j << std::endl;

}

Need Your Help

How to Target Python Server File Using Serial

python sockets arduino

I had a question regarding whether it is possible to directly connect to an IP using Python's socket module. I've tested the code below locally and everything works fine, but have issues when bring...

Chossing between visual c++ and gcc to compile c++ programs mainly for studying purposes

gcc visual-studio-2013

I am moving from g++ to visual studio mainly for c++ coding , i wanted to ask that whether microsoft's c++ compiler supports all the features of c++11 and c++14 as supported by g++ .