Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding members to a template class specialization?

Daniel Pitts
Guest
 
Posts: n/a
#1: Jun 27 '08
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<*,3to add
template<typename component_t>
Vector<component_t, 3Vector<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, 3crossProduct(const Vector<component_t, 3&a,
const Vector<component_t, 3&b) {...}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Victor Bazarov
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Adding members to a template class specialization?


Daniel Pitts wrote:
Quote:
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<*,3to add
template<typename component_t>
Vector<component_t, 3Vector<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.
Yes, it's possible. You basically need to repeat all other stuff,
unfortunately.
Quote:
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?
Again, yes, it is. You will face the fact that the rest of the class
would have to be essentially repeated.
Quote:
template<typename component_t>
Vector<component_t, 3crossProduct(const Vector<component_t, 3&a,
const Vector<component_t, 3&b) {...}
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 dimclass 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 dimclass Vector
: public VectorBase<C, dim{
// nothing here
};

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Daniel Pitts
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Adding members to a template class specialization?


Victor Bazarov wrote:
Quote:
Daniel Pitts wrote:
Quote:
>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<*,3to add
>template<typename component_t>
>Vector<component_t, 3Vector<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.
>
Yes, it's possible. You basically need to repeat all other stuff,
unfortunately.
>
Quote:
>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?
>
Again, yes, it is. You will face the fact that the rest of the class
would have to be essentially repeated.
>
Quote:
>template<typename component_t>
>Vector<component_t, 3crossProduct(const Vector<component_t, 3&a,
>const Vector<component_t, 3&b) {...}
>
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 dimclass 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 dimclass Vector
: public VectorBase<C, dim{
// nothing here
};
>
template<class Cclass Vector<C,3>
: public VectorBase<C,3{
Vector crossProduct(Vector const&) const;
};
>
V
Thanks, that is exactly what I wanted to know :-)


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Fei Liu
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Adding members to a template class specialization?


Daniel Pitts wrote:
Quote:
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<*,3to add
template<typename component_t>
Vector<component_t, 3Vector<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, 3crossProduct(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, 3crossProduct(const Vector<component_t, 3&other) const
{
BOOST_STATIC_ASSERT(dimensions == 3);
...
}
};
Closed Thread