Hello!
How should I handle the following situation :
I am writing my own 2D vector class and have the following code:
class Vector2 : public (...)
public:
Vector2(float x, float y) {
local_vector_storage_[0] = x;
local_vector_storage_[1] = y;
}
template <typename Iterator> Vector2(Iterator begin, Iterator end) {
ASSERT(end - begin == 2);
resize(2);
std::copy(begin, end, local_vector_storage_.begin());
}
// ...
};
Now if I say Vector2 v(3.0f, 4.0f);
it compiles fine and calls the appropriate float constructor.
But if I write Vector2 v(3, 4);
it fails because the templated iterator constructor "fits better" and Vector2(Iterator(3), Iterator(4))
is called.
What should I do in this case?
My idea was about introducing assign(It1, It2)
member method instead of the constructor but maybe there is a better solution?
Edit:
Also, what do you think about ASSERT(end - begin == 2)
line? I know that this means I can't, for example, pass iterators of std::list
, but brings additional safety. Should I do this or not?