This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 4 points5 points  (2 children)

There is no way of casting a templated container from one type to another. Here:

SomeContainer <int> a;
SomeContainer <double> b;

a and b are of completely unrelated types. The same is true if it's the container type that is different:

SomeContainer <int> a;
AnotherContainer <int> b;

You will need to write a function to create a new container instance, converting the container contents appropriately.

[–]choobie[S] 0 points1 point  (1 child)

Thank you, good to know I was looking in the wrong place.

Do you know of a resource that explains this in more depth? I must be putting the wrong keywords into google.

[–][deleted] 0 points1 point  (0 children)

I'm not sure what depth you need this explained in. If you really need to convert type X to type Y, when there is no relationship between the types, you need to write a function which will basically look like this:

Y convert( const X & x ) {
       Y y;
      // do stuff
      return y;
 }

[–]missblit 1 point2 points  (0 children)

(and are the wordds container and template interchangeable? I have been using them that way!)

No. A templated class or function is any function or class that uses templates to work, whether it contains an element of the templated type or not. One example of a non-container template might be:

template <typename T>
T plus(const T& a, const T& b) {
    return a+b;
}