oh that's an easy one; you should know that! the native types:
int, char, unsigned int, float, double, long, etc.
oh that's an easy one; you should know that! the native types:
int, char, unsigned int, float, double, long, etc.
Requiring the type to have all of those operators is probably too strict a requirement, since !=, +, and - can be represented in terms of the other ones.
If you see a != b, you can replace with !(a == b)
If you see a + b, you can replace with a temporary which is copy-constructed or assigned from a, then invoke += b on it.
Similar for a - b.
I would only require =, ==, +=, -=
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
One way to avoid the problem of operators not implemented by the class is to simply use a template function. That way, as long as the user doesn't try to invoke the operator, no error occurs. For example:
Yep, excellent suggestion.Code:template < typename T > struct test { template < typename U > test& operator *= ( U const& rhs ) { value *= rhs; return *this; } T value; }; int main( void ) { test< string > ts; // fine ts *= string( ); // error }
okay, I have another question now regarding operators between two vectors of different size:
What does this sentence mean:
If you check for mismatched sizes, but it is impossible for the sizes to be mismatched based on strong typing (i.e., the compiler will fail)
Can you give me an example of when this could happen and how to prevent this in my vector template class.
I'm not sure what that actually means, honestly. Can you provide the complete requirements (or at least a link to it)?
The full requirements:
When operations between vectors of unequal size are performed, exceptions should be thrown, or the compile should fail. If you check for mismatched sizes, but it is impossible for the sizes to
be mismatched based on strong typing (i.e., the compiler will fail),
No, I mean the full requirements for the entire assignment. That might aid in infering the meaning from the context of everything else. From what I can tell, checking for mismatched sizes doesn't even require an operation on the element type, so there should never be a complication error in the first place. I was hoping that maybe the rest of requirements might shed some light on the statement in question.
I can't copy the whole assignment here.
That is already the case with the member functions of the class template.Originally Posted by Sebastiani
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
it's important to remember that classes instantiated from the same template, but with different template parameters are totally separate types. they are not related to each other. a foo<int,2> is a different type than a foo<int,3>.
thus any template class method that accepts expects its own type as a function parameter cannot accept a class instantiated from the same template with different parameters.
do you follow?
the reason to have the type take an int is so that I can do the following:
it is said to initialize temp to 0, but how can this be possible with a type taking an int?Code:vector<class, 2> temp; vector<class, 2> temp 2; temp2[0] = 1; temp2[1] = 2; vector<int, 2> result = temp + temp2;