template<typename T, size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin() { return v_; }
iterator end() { return v_+size; }
const_iterator begin() const { return v_; }
const_iterator end() const { return v_+size; }
private:
T v_[size];
};
template<typename T, size_t size>
class fixed_vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
fixed_vector() {}
/************************************************************************/
// add
fixed_vector() : v_(new T[size]) {}
~fixed_vector() { delete [] v_; }
/************************************************************************/
template<typename O, size_t osize>
fixed_vector(const fixed_vector<O, osize>& other)
{
copy(other.begin(), other.begin() + min(size, osize), begin());
}
// add
/************************************************************************/
fixed_vector(const fixed_vector<O, osize>& other) : v_(new T[size])
{
try
{
copy(other.begin(), other.begin() + min(size, osize), begin());
}
catch (...)
{
delete [] v_;
throw;
}
}
fixed_vector(const fixed_vector<T, size>& other) : v_(new T[size])
{
try
{
copy(other.begin(), other.end(), begin());
}
catch (...)
{
delete [] v_;
throw;
}
}
/************************************************************************/
template<class RAIter>
fixed_vector(RAIter first, RAIter last)
{
copy(first, first + min(size, (size_t)last - first), begin());
}
template<typename O, size_t osize>
fixed_vector<T, size>& operator=(const fixed_vector<O, osize>& other)
{
// If one of the T assignments fails during the copy() operation ?
copy(other.begin(), other.begin() + min(size, osize), begin());
return *this;
}
void Swap(fixed_vector<T, size>& other) throw()
{
swap(v_, other.v_);
}
fixed_vector<T, size>& operator=(const fixed_vector<O, osize>& other)
{
fixed_vector<T, size> temp(other);
Swap(temp);
return *this;
}
fixed_vector<T, size>& operator=(const fixed_vector<T, size>& other)
{
fixed_vector<T, size> temp(other);
Swap(temp);
return *this;
}
template<class RAIter>
fixed_vector<T, size>& assign(RAIter first, RAIter last)
{
copy(first, first + min(size, (size_t)last - first), begin());
return *this;
}
iterator begin() { return v_; }
iterator end() { return v_+size; }
const_iterator begin() const { return v_; }
const_iterator end() const { return v_+size; }
private:
T v_[size];
};
/*
struct X
{
template<typename T>
X( const T& ); // NOT copy constructor, T can't be X
template<typename T>
perator=( const T& ); // NOT copy assignment, T can't be X
};
*/
fixed_vector<char,4> v;
fixed_vector<int,4> w(v); // templated construction
w = v; // templated assignment
class B { /*...*/ };
class D : public B { /*...*/ };
fixed_vector<D*,4> x;
fixed_vector<B*,4> y(x); // templated construction
y = x; // templated assignment
fixed_vector<D*,16> x;
fixed_vector<B*,42> y(x); // initializes using 16 values
y = x; // assigns using 16 values
int main()
{
fixed_vector<int, 4> i4f;
fixed_vector<char, 4> i4f2(i4f);
return 0;
}