C++ 如何在模板类范围内定义模板运算符?

C++ 如何在模板类范围内定义模板运算符?,c++,templates,C++,Templates,我有一个矩阵类的问题,我试图“模板化”。其思想是能够将两个不同类型的矩阵相乘,而无需显式重载乘法方法。例如,假设我们有一个类型a和一个类型B。如果我乘一个矩阵 请温柔一点,这是我第一次冒险 附加问题:typename std::common_type::type是否每次在代码中出现时都进行求值,或者是否以某种方式记住了结果?我可能会对大型矩阵进行乘法,并且我希望避免对typename std::common_type::type求值数千次以获得恒定的结果 编辑:这里给出的示例只是一个复制错误的最

我有一个矩阵类的问题,我试图“模板化”。其思想是能够将两个不同类型的矩阵相乘,而无需显式重载乘法方法。例如,假设我们有一个
类型a
和一个
类型B
。如果我乘一个
矩阵
请温柔一点,这是我第一次冒险

附加问题:typename std::common_type::type是否每次在代码中出现时都进行求值,或者是否以某种方式记住了结果?我可能会对大型矩阵进行乘法,并且我希望避免对typename std::common_type::type求值数千次以获得恒定的结果

编辑:这里给出的示例只是一个复制错误的最小代码


EDIT2:类定义中的注释运算符*=只是迄今为止我试图绕过错误的各种方法的一个痕迹

您遇到了一些问题。我认为这些变化是不言自明的。如果没有,请告诉我。 锁销:

#包括
#包括
#包括
模板
类矩阵
{
私人:
T1表;
公众:
常量T1&getTable()常量
{
返回表;
}
T1&getTable()
{
返回表;
}
矩阵(T1 m_表);
模板
矩阵和运算符*=(常数矩阵和);
};
模板
矩阵运算符*(矩阵和,矩阵和);
//模板
//模板
//矩阵和矩阵::运算符*=(常数矩阵和B);
模板
矩阵::矩阵(T1 m_表):表(m_表)
{
}
模板
矩阵运算符*(矩阵与M2、矩阵与M1)
{   
typename std::common_type::type tab3=(M1.getTable())*(M2.getTable());
返回矩阵(表3);
}
模板
模板
矩阵和矩阵::运算符*=(常数矩阵和B)
{
table=(T1)table*(B.getTable());
归还*这个;
}
int main()
{
浮动表1(12.5);
int tab2(11);
矩阵M1(表1);
基质M2(表2);
矩阵M3=M1*M2;

std::cout您注释掉的
运算符*=
返回一个
矩阵&
,而您定义的运算符有一个
void
返回类型。(此外,它们都应该将其参数作为常量引用。)无关,但让矩阵由单个元素组成有什么意义?也许是为了MRE。@NokiYola你能接受我下面的回答吗?谢谢。你在评论中感谢了我,但现在你的评论消失了,你删除了吗?@DavidBien我接受了。很抱歉,我不知道我可以(也不必)这样做接受答案。不,我没有删除我的评论,我想知道它们是如何消失的。我在之前的评论中感谢你,但我也问你是否知道
typename std::common_type::type
是否会在for循环的每次迭代中进行评估。我还问你带来的修改是否精确(特别是
常量T1和getTable()常量的第一个
常量
,以及
模板
模板
之间的差异)
#ifndef MATRICE_H
#define MATRICE_H

#include <typeinfo>
#include <type_traits>
#include <iostream>

template<typename T1>
class Matrice
{
private:
    T1 table;

public:
    T1 getTable(){return table;}
    Matrice(T1 m_table);

    //template<typename T2>
    //Matrice<T1>& operator *= (Matrice<T2>&);
};

template<typename T1, typename T2, typename T3>
Matrice<T3> operator*(Matrice<T2>&, Matrice<T1>&);

template<typename T1, typename T2>
Matrice<T1>& Matrice<T1>::operator *= (const Matrice<T2>& B);

#endif
#include "Matrice.h"

template<typename T1>
Matrice<T1>::Matrice(T1 m_table):table(m_table)
{
}

template<typename T1, typename T2>
Matrice<typename std::common_type<T1,T2>::type> operator * (Matrice<T2>& M2, Matrice<T1>& M1)
{   
    
    typename std::common_type<T1,T2>::type tab3= (M1.getTable())*(M2.getTable());
    return Matrice<typename std::common_type<T1,T2>::type>(tab3);
}

template<typename T1, typename T2>
Matrice<T1>& Matrice<T1>::operator *= (const Matrice<T2>& B)
{
    table = (typename T1) table*(B.getTable())
}

int main()
{
    float tab1(12.5);
    int tab2(11);
    Matrice<float> M1(tab1);
    Matrice<int> M2(tab2);
    Matrice<float> M3 =  M1*M2;
    std::cout << M3.getTable() << std::endl;
    M3*=M2;
    return 0;
}
Matrice.h:26:6: error: no declaration matches 'void Matrice<T1>::operator*=(Matrice<T2>&)'
   26 | void Matrice<T1>::operator *= (Matrice<T2>& B);

Matrice.cpp:18:6: error: no declaration matches 'void Matrice<T1>::operator*=(Matrice<T2>&)'
   18 | void Matrice<T1>::operator *= (Matrice<T2>& B)
Matrice.cpp: In function 'int main()':
Matrice.cpp:31:7: error: no match for 'operator*=' (operand types are 'Matrice<float>' and 'Matrice<int>')
   31 |     M3*=M2;
#include <typeinfo>
#include <type_traits>
#include <iostream>

template<typename T1>
class Matrice
{
private:
    T1 table;

public:
    const T1 & getTable() const
    {
        return table;
    }
    T1 & getTable()
    {
        return table;
    }
    Matrice(T1 m_table);

    template<typename T2>
    Matrice<T1>& operator *= ( const Matrice<T2>& );
};

template<typename T1, typename T2, typename T3>
Matrice<T3> operator*(Matrice<T2>&, Matrice<T1>&);

// template<typename T1>
// template<typename T2>
// Matrice<T1>& Matrice<T1>::operator *= (const Matrice<T2>& B);

template<typename T1>
Matrice<T1>::Matrice(T1 m_table):table(m_table)
{
}

template<typename T1, typename T2>
Matrice<typename std::common_type<T1,T2>::type> operator * (Matrice<T2>& M2, Matrice<T1>& M1)
{   
    
    typename std::common_type<T1,T2>::type tab3= (M1.getTable())*(M2.getTable());
    return Matrice<typename std::common_type<T1,T2>::type>(tab3);
}

template<typename T1>
template<typename T2>
Matrice<T1>& Matrice<T1>::operator *= (const Matrice<T2>& B)
{
    table = (T1) table*(B.getTable());
    return *this;
}

int main()
{
    float tab1(12.5);
    int tab2(11);
    Matrice<float> M1(tab1);
    Matrice<int> M2(tab2);
    Matrice<float> M3 =  M1*M2;
    std::cout << M3.getTable() << std::endl;
    M3*=M2;
    return 0;
}