Since We're Talking Programming, a Hopefully Quick C++ Question

Templates… still confuse me. I want to do something like this:

template <Class NumericType>
class Vector3D
     NumericType x,y,z;
   Bunch of functions that operate on 3D vectors via the rules of math.
  Vector3D<NumericType> operator+(const& Vector3d<OTHERNumericType)

So, for example, I can appropriately add a 3D vector of floats with a 3D vector of ints (implicitly converting the int to float first).

Also I want to be able to do something like:

template class<SubsetOfNumericType>
class Container
    Container<SubsetofNumericType>  operator+(const& Container<OtherSubsetofNumericType>)

Where the operator+ of Container would call the underlying addition operator of the Vector3D of the appropriate types.

I’m a bit lost here. From where I sit this looks like it’s going to be more complex to do than to simply program out the three-four numeric types explicitly (float, double, int at least…). This seems like I’ve missed something since Templates seem to be designed to avoid having to do the latter, but I’m having trouble tracking down references about how to try to disambiguate overloaded operators of a template. I think I’m missing a basic concept. Do I just explicitly need to somehow define:


Pretty sure that’s not the right syntax, since I’m explicitly defining the input type for the template class, but I think the concept is clear enough there.

Any pointers to how to do this/what term I should be looking for? Or is this one of those things like template metaprogramming that’s 99% of the time more work than it’s worth?

  1. Implicit conversion is bad, because there’s all sorts of you’re-kidding-me ways to accidentally call it. Disable that with the explicit keyword.
  2. You can define conversion functions in the class that will correctly retype the class for compatibility with other templated types of itself.

You do not want to do implicit conversion. That way lies madness (which is why compilers warn about it).

By implicit conversion in this case you mean something like

Vector3D<int> V1
Vector3D<float> V2, V3


The problem is I don’t understand why this is bad or implicit.

It’s a template, so I’ve had to explicitly design the conversion routines, and others aren’t defined. Is the difficulty the worry that somehow I would have a Vector3D of, say, address pointers and the compiler would cast them into INT and then from there use the implicit conversion into operations there? It just seems a really unlikely case to occur.

I guess I just never considered it to be implicit conversion since I have to define the operators and the types it works on. Particularly since I’d only have operators for the types I want to use.

Implicit conversion is bad because unintuitive things can happen behind your back. This covers the generalities:

Technically your example isn’t implicit conversion if you’ve defined all the conversions, but there is implicit conversion if you allow the compiler to handle the float to int conversions automatically. A lot of gnarly stuff can go on behind the scenes when that happens.