Adding members to a template class specialization?

D

Daniel Pitts

I have a Vector template class (math vector), where the number of
dimensions is one of the template parameters.

template<typename component_t, int dimensions>
class Vector {...}

I'd like to specialize for Vector<*,3> to add
template<typename component_t>
Vector<component_t, 3> Vector<component_t, 3>::crossProduct(const
Vector<component_t, 3> &other) const {...}

Since cross product only makes sense with vectors that have dimension=3.

Is this possible? Basically, I want a compile time error if you call
crossProduct on a vector that isn't 3d.

Hmm, I think I figured out I can do it as a friend/external function,
but I'd like to try to do it as a member function. Is that possible?

template<typename component_t>
Vector<component_t, 3> crossProduct(const Vector<component_t, 3> &a,
const Vector<component_t, 3> &b) {...}
 
D

Daniel Pitts

Victor said:
Yes, it's possible. You basically need to repeat all other stuff,
unfortunately.


Again, yes, it is. You will face the fact that the rest of the class
would have to be essentially repeated.


Since you want to avoid repeating not only the declarations but also the
implementations of the class, you are better off extracting the common
part between those two Vector classes into a separate base class,
something like

template<class C, unsigned dim> class VectorBase {
... common stuff goes here ...
Vector& operator +=(Vector const&) {
return *this;
}
};

and then derive the generic vector and the specialisation (for dim==3)
from that base:

template<class C, unsigned dim> class Vector
: public VectorBase<C, dim> {
// nothing here
};

template<class C> class Vector<C,3>
: public VectorBase<C,3> {
Vector crossProduct(Vector const&) const;
};

V

Thanks, that is exactly what I wanted to know :)
 
F

Fei Liu

Daniel said:
I have a Vector template class (math vector), where the number of
dimensions is one of the template parameters.

template<typename component_t, int dimensions>
class Vector {...}

I'd like to specialize for Vector<*,3> to add
template<typename component_t>
Vector<component_t, 3> Vector<component_t, 3>::crossProduct(const
Vector<component_t, 3> &other) const {...}

Since cross product only makes sense with vectors that have dimension=3.

Is this possible? Basically, I want a compile time error if you call
crossProduct on a vector that isn't 3d.

Hmm, I think I figured out I can do it as a friend/external function,
but I'd like to try to do it as a member function. Is that possible?

template<typename component_t>
Vector<component_t, 3> crossProduct(const Vector<component_t, 3> &a,
const Vector<component_t, 3> &b) {...}
If you are just looking for a way that would issue compile time error if
crossProduct is called on a vector that isn't 3d. there is a easier way:

template <typename T, int dimensions>
class Vector{

Vector<T, 3> crossProduct(const Vector<component_t, 3> &other) const
{
BOOST_STATIC_ASSERT(dimensions == 3);
...
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top