I am working on a simple matrix template class for practice purposes, I have defined my constructor and destructor but on testing the class, I found out that Move and copy operations works without any flaws and I didn't define them. I really do not understand what's going and I don't trust what is happening.
#ifndef MATRIX_H_
#define MATRIX_H_
#include <cstdlib>
#include <iostream>
namespace mat
{
template<class T>
class Matrix
{
public:
Matrix(size_t row, size_t column, const T& val)
: row_{row}, column_{column}, elem_{static_cast<T*>(::operator new(row_ * column_))}
{
for(size_t i = 0; i != size(); ++i )
elem_[i] = val;
}
Matrix(size_t row, size_t column)
: row_{row}, column_{column}, elem_{static_cast<T*>(::operator new(row_ * column_))} {}
size_t size() const { return row_ * column_; }
~Matrix()
{
for(size_t i = size(); i != 0; --i)
{
elem_[i].~T();
delete elem_;
}
}
private:
size_t row_;
size_t column_;
T *elem_;
};
}
#endif
#include <iostream>
#include "Matrix.h"
using namespace mat;
class SomeClass
{
int s_;
public:
SomeClass(int s ) : s_{s} {}
};
template<class T>
Matrix<T> fn(const Matrix<T> A)
{
return A;
}
int main()
{
SomeClass s{1};
Matrix<SomeClass> my_mat(2, 3, s);
Matrix<int> my_mat2(2, 3, 4);
Matrix<SomeClass> my_mat3{2, 3};
Matrix<int> copy_mat(my_mat2);
}