you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?
the += is just an operator overloading to add two vectors
Printable View
you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?
the += is just an operator overloading to add two vectors
The partial example that I gave does not make use of that. What it does make use of is a copy assignment operator, which I assume is among "the full set of operators provided below". If it is not... then your best option is to use a std::vector instead of an array of N elements (and if you are not allowed to use std::vector, then you should use placement new, and everything just becomes more complicated).Quote:
Originally Posted by -EquinoX-
Sure, but what does "add two vectors" mean? You have to be clear on that, since it could mean the pairwise addition of the elements of the two vectors, or it could mean the concatenation of one vector to the other, or something else.Quote:
Originally Posted by -EquinoX-
I guess it would add each element that is the vector with each element on the other vector, if the size doesn't match it would throw an exception..
here's the continuation of the instruction, if you wish to know the full set of operators provided below are:
Your template class should provide at least the following class or global operator methods:
1 =, ==, !=, +, +=, -, -=, [ ]. Both binary, and unary, operator- should be provided.
2 In operations with scalar type values, the operators *, /, *=, /= can be used.
which raises up a new question, how can the element type also have the following operators...
Don't you want something like
orCode:vector& operator*=(const vector& other)
{
if (this->size != other.size)
throw std::length_error("No matching size");
for(int i=0; i < this->size; ++i)
this->elements[i] *= other.elements[i];
return *this;
}
Code:vector& operator*=(const vector& other)
{
for(int i=0; i < this->size && i < other.size; ++i)
this->elements[i] *= other.elements[i];
return *this;
}
but it only says for operation with scalar values... so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this
anyone care to give me some insights?
Code:template<typename T,size_t N>foo
{
private:
T arr[N];
public:
foo& operator*=(T t)
{
for(size_t i=0;i<N;++i)
{
arr[i]*=t;
}
return *this;
}
};
what if what's stored in i can't be multiplied by t? what if T is a char or a string?
also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas?
A char is a scalar.Quote:
Originally Posted by -EquinoX-
Then that is the user's problem (though in the case of char or any integer/floating point type, overflow is also a problem, but at run time). In some later version of C++ there may be the concept of concepts, where it can be clearly enforced that a template can only be instantiated with a type that supports certain operations, but for now (and perhaps even in C++0x) such a feature does not exist, though Boost.ConceptCheck makes an attempt at it.Quote:
Originally Posted by -EquinoX-
It is the other way round: you tell your users: my template works with types that support these operations with these semantics. If you instantiate it with a type that does not support these operations or with the wrong semantics, good luck.Quote:
Originally Posted by -EquinoX-
look at std::valarray.
I think the only way I can restraint it is for the template class vector to take an object type... type can be anything but then I can guarantee that type has all the overload operators
no you can't. you're just kicking the can down the road.
the point of making that restriction is that your vector class isn't supposed to be used with any type!
if you make a Type<std::string> you're going to get a compilation error in Type just the same as you with vector<std::string> if you ever use *= operator of the class.
you're still looking at the phrasing of the assignment backwards:
you're looking at this as a requirement of your class to provide a way to support those operators no matter what stupid thing gets specified as a template parameter type.Quote:
2 In operations with scalar type values, the operators *, /, *=, /= can be used.
what that really means is that is the operators that can be used with your vector - it's a requirement of the parameter types, not your vector! you must provide them and use the parameter type's operator, but you are not supposed to try to support an operator that doesn't exist.
besides, you'll see that eventually, if you ever intend to use those operators, the underlying paramter type must have them implemented anyway, so your strategy just adds a layer of complication with no gain over the simpler implementation i am suggesting to you.
Code:#include <valarray>
int _tmain(int argc, _TCHAR* argv[])
{
std::valarray<std::string>thisMakesNoSense;
thisMakesNoSense*="asdf";
return 0;
}
well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators