Skip to main content
gamedev.net
🔒 Locked

Reducing template scope

Started by jesterlecodeur 2 replies 450+ views
Original Post
jesterlecodeur
jesterlecodeur
Image this code:
template< class ta_type > 
inline	CVector2::CVector2( const ta_type& A )
{ x = A[0]; y = A[1]; }
 
I want to allow only some type (e.g. not float), is there a way to do whithout writing a prototype for each?
_______________
Jester, studient programmerThe Jester Home in French
Void
Void
To limit template parameters, either

1. Use a traits class as template parameter. See www.boost.org

2. Explicitly specialize the template for the type you want, leave the generic template undefined.
Shannon Barber
Shannon Barber
3. Make it fail to compile with the unwanted types, or ASSERT in a debug build stating that the template won't work with this type. e.g. Explicitly specialize for the type you _don't_ want.

    
//CVector2<float> class are NOT allowed!

class CVector2<float>
{
private:
class hack
{
int i;
};
public:
CVector2<float>(hack h)
{
}
};

That will prevent the construction of a CVector2 with float as the type.

If you just don't want a float passed as a parameter, specialize the method with float as the passed type, and assert in it.

Edited by - Magmai Kai Holmlor on January 5, 2002 11:22:40 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.