Why std::complex is not an arithmetic type?

c++, complex-numbers, types

Solution

The `arithmetic` in `is_arithmetic` is a misnomer. Or rather, it's a C++-nomer. It doesn't mean the same thing as it means in English. It just means it's one of the built-in numeric types(int, float, etc...). `std::complex` is not a built-in, it is a class.

Do you really need that `static_assert`? Why not just let the user try it with any type? If the type doesn't support the needed operations, then tough luck.

Problem

I created the following Matrix class: ``` template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,""); public: Matrix(size_t n_rows, size_t n_cols); Matrix(size_t n_rows, size_t n_cols, const T& value); void fill(const T& value); size_t n_rows() const; size_t n_cols() const; void print(std::ostream& out) const; T& operator()(size_t row_index, size_t col_index); T operator()(size_t row_index, size_t col_index) const; bool operator==(const Matrix<T>& matrix) const; bool operator!=(const Matrix<T>& matrix) const; Matrix<T>& operator+=(const Matrix<T>& matrix); Matrix<T>& operator-=(const Matrix<T>& matrix); Matrix<T> operator+(const Matrix<T>& matrix) const; Matrix<T> operator-(const Matrix<T>& matrix) const; Matrix<T>& operator*=(const T& value); Matrix<T>& operator*=(const Matrix<T>& matrix); Matrix<T> operator*(const Matrix<T>& matrix) const; private: size_t rows; size_t cols; std::vector<T> data; }; ``` I tried to use a matrix of std::complex: ``` Matrix<std::complex<double>> m1(3,3); ``` The problem is that the compilation fails (static_assert fails): ``` $ make g++-mp-4.7 -std=c++11 -c -o testMatrix.o testMatrix.cpp In file included from testMatrix.cpp:1:0: Matrix.h: In instantiation of 'class Matrix<std::complex<double> >': testMatrix.cpp:11:33: required from here Matrix.h:12:2: error: static assertion failed: make: *** [testMatrix.o] Error 1 ``` Why std::complex is not an arithmetic type? I want to enable the utilisation of unsigned int (N), int (Z), double (R), std::complex (C) and maybe some home made class (e.g. a class representing Q)... It is possible to obtain this behave? EDIT 1: If I remove `static_assert` the class works normally. ``` Matrix<std::complex<double>> m1(3,3); m1.fill(std::complex<double>(1.,1.)); cout << m1 << endl; ```

Original source