Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:35:50+00:00 2026-06-10T07:35:50+00:00

I created a matrix class with templates: template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,);

  • 0

I created a matrix class with templates:

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);

    // Functions

    // Operators
Matrix<T>& operator*=(const T& value)

private:
    size_t rows;
    size_t cols;
    std::vector<T> data;
};

I created the following two (external) operators to multiply my matrix with a number:

// Inner operator used by the externals ones
template <typename T>
inline Matrix<T>& Matrix<T>::operator*=(const T& value)
{
    for(size_t i(0); i < data.size(); i++)
    {
        data[i] *= value;
    }

    return *this;
}

template <typename T>
inline Matrix<T> operator*(const T& value, const Matrix<T>& matrix)
{
    Matrix<T> tmp(matrix);

    return tmp *= value;
}

template <typename T>
inline Matrix<T> operator*(const Matrix<T>& matrix, const T& value)
{
    return value * matrix;
}

The problem is that if I declared the matrix as a double, I can multiply the matrix only by doubles and so on…

Matrix<double> m1(3,3,1.);

5. * m1; // Works
5 * m1; // Doesn't work (5 is an int and not a double)

How can I fix this behave? It is possible to let doubles be multiplied by others arithmetic types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:35:51+00:00Added an answer on June 10, 2026 at 7:35 am

    Sure, just allow two parameters to your templated free functions and member functions.

    For example:

    template <typename T> class Matrix {
      /* ... */
      template <typename U>
      inline Matrix<T>& operator*=(const U& value)
      {
        for(size_t i(0); i < data.size(); i++)
        {
            data[i] *= value;
        }
    
        return *this;
      }
    };
    
    template <typename T, typename U>
    inline Matrix<T> operator*(const U& value, const Matrix<T>& matrix)
    {
        Matrix<T> tmp(matrix);
    
        return tmp *= value;
    }
    

    This will trigger compiletime errors if you try to multiply your Matrix with something nonsensical, that is, if T*U is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a matrix class: template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,); public: Matrix(size_t
I created the following Matrix class: template <typename T> class Matrix { static_assert(std::is_arithmetic<T>::value,); public:
I created a matrix class: template <typename T> class Matrix { public: Matrix(size_t n_rows,
I created a Matrix class, using static_assert(std::is_arithmetic<T>::value,); to check if the template type is
I have a class template<size_t N, size_t M> class Matrix { // .... };
I have a template class which implements function: template<typename T> class Matrix { ...
I recently created a generic Matrix<T> class that acts as a wrapper around a
I am coding a matrix template class and the problem I am having is
I am working on an exercise in operator overloading. I've created a matrix class
So I have a generic Matrix class that I created which has the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.