繁体   English   中英

C ++矩阵模板,矩阵矩阵与矩阵数乘法之间的歧义

[英]C++ Matrix template, ambiguity between matrix-matrix and matrix-number multiplication

我正在写一个矩阵模板类。 一切顺利,直到我使乘法运算符超载为止。 我的课看起来像这样:

template <typename TNum> class Matrix
{
private:
    // ...
    TNum* Data;
public:
    const TMatIdx NRows; // Type TMatIdx defined somewhere else.
    const TMatIdx NCols;
    const TMatIdx Size;

    // ...
    // Matrix * matrix
    template <typename T2>
    const Matrix<TNum> operator*(const Matrix<T2>& right) const;

    // Matrix * number
    template <typename T2>
    Matrix<TNum>& operator*=(const T2& scale);
};

// Matrix * number
template <typename TNum, typename T2>
Matrix<TNum> operator*(Matrix<TNum> lhs, const T2& rhs);
// Number * matrix
template <typename TNum, typename T2>
Matrix<TNum> operator*(const T2& lhs, Matrix<TNum> rhs);

我希望使用相同的*运算符覆盖矩阵和数字之间所有可能的乘法组合。

然后,我编写了一个小的测试程序,将两个Matrix<double>相乘,我的clang ++编译器抱怨模棱两可:

test.cpp:46: error: ambiguous overload for 'operator*' in 'M * N'
matrix.h:225: note: candidates are: const QCD::Matrix<TNum> QCD::Matrix<TNum>::operator*(const QCD::Matrix<T2>&) const [with T2 = double, TNum = double]
matrix.h:118: note:                 QCD::Matrix<TNum> QCD::operator*(const T2&, QCD::Matrix<TNum>) [with TNum = double, T2 = QCD::Matrix<double>]
matrix.h:109: note:                 QCD::Matrix<TNum> QCD::operator*(QCD::Matrix<TNum>, const T2&) [with TNum = double, T2 = QCD::Matrix<double>]
test.cpp:52: error: ambiguous overload for 'operator*' in 'M * N'
matrix.h:225: note: candidates are: const QCD::Matrix<TNum> QCD::Matrix<TNum>::operator*(const QCD::Matrix<T2>&) const [with T2 = double, TNum = double]
matrix.h:118: note:                 QCD::Matrix<TNum> QCD::operator*(const T2&, QCD::Matrix<TNum>) [with TNum = double, T2 = QCD::Matrix<double>]
matrix.h:109: note:                 QCD::Matrix<TNum> QCD::operator*(QCD::Matrix<TNum>, const T2&) [with TNum = double, T2 = QCD::Matrix<double>]

是否有可能克服这种歧义而不必明确写下T2的所有可能专业化知识?

仅供参考,这是我的实现:

template<typename TNum> template <typename T2>
Matrix<TNum>& Matrix<TNum> ::
operator*=(const T2& rhs)
{
    for(TMatIdx i = 0; i < Size; i++)
        Data[i] *= rhs;
    return *this;
}

template<typename TNum> template <typename T2>
const Matrix<TNum> Matrix<TNum> ::
operator*(const Matrix<T2>& right) const
{
    Matrix<TNum> c(NRows, right.NCols);
    TNum sum_elems;
    for(TMatIdx i = 0; i < NRows; i++)
    {
        for(TMatIdx j = 0; j < right.NCols; j++)
        {
            sum_elems = TNum(0);
            for(TMatIdx k = 0; k < right.NRows; k++)
            {
                sum_elems += at(i, k) * right.at(k, j);
            }

            c.at(i, j) = sum_elems;
        }
    }
    return c;
}


template <typename TNum, typename T2>
Matrix<TNum> operator*(Matrix<TNum> lhs, const T2& rhs)
{
    lhs *= rhs;
    return lhs;
}

template <typename TNum, typename T2>
Matrix<TNum> operator*(const T2& lhs, Matrix<TNum> rhs)
{
    rhs *= lhs;
    return rhs;
}

我将描述一个使用c ++ 11的解决方案,然后解释如何在c ++ 98中实现它。

在c ++ 11中,标头<type_traits>包括类型函数和类型谓词。 这使强制约束更加方便。

如果T1T2类型相同std::is_same<T1, T2>::value为true,否则为false。

如果bool为true,则typename std::enable_if< bool, T >::type是定义良好的类型T ,否则定义不正确。

当编译器寻找候选模板函数和方法时,如果尝试的专业化失败,它不会引发错误。 它只是淘汰了那个候选人。 这意味着以下代码将消除歧义:

template <typename TNum, typename T2>
typename std::enable_if< (!std::is_same<Matrix<TNum>, T2>::value),
Matrix<TNum> >::type operator*(const T2& lhs, Matrix<TNum> rhs);

做出此决定时仅考虑声明。 上面的逻辑在语义上是合理的,但是让人讨厌阅读。 因此,c ++ 11支持模板别名和constexpr函数。

template<bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;

template<typename T1, typename T2>
constexpr bool Is_same(){
  return std::is_same<T1, T2>::value;
}

上面的内容变为:

template <typename TNum, typename T2>
Enable_if<( !Is_same<Matrix<TNum>, T2>() ),
Matrix<TNum> > operator*(const T2& lhs, Matrix<TNum> rhs);

概念将提供工具,以使其更方便。

现在,如果您没有c ++ 11,那么您就不会眼前一亮。 但是,Boost提供了相同的功能。 假设您也没有,实施它们并不可怕。

编译时函数取决于多种语言规则,这使它们难以理解。 我们将首先考虑enable_if 我们希望对typename enable_if<true, T>::type进行良好定义,但对typename enable_if<true, T>::type typename enable_if<false, T>::type作废。 我们使用专业化:

template<bool B, typename T = void>
struct enable_if {
  typedef T type;
};

template<typename T>
struct enable_if<false, T> {};

注意虚假处理的只是相关案例的一半。 值得一读。

为了实现is_same ,我们需要在编译时使用true或false的概念。 我们可以使用静态const变量来保证这一点。 每当其模板参数相同时,我们希望is_same具有编译时为true的值。 专业化系统规则直接处理此问题。

template<typename, typename>
struct is_same{
  static const bool value = false;
};

template<typename T>
struct is_same<T, T>{
  static const bool value = true;
};

这应该做您想要的。 请注意,您可以进一步抽象一个步骤,并制作另一对结构。

struct false_type {
  static const bool value = false;
};

struct true_type {
  static const bool value = true;
};

然后is_same变成:

template<typename, typename>
struct is_same : false_type {};

template<typename T>
struct is_same<T, T> : true_type {};

这使其看起来更像一个函数。

我更喜欢类别解决方案,因为将元程序分离为头文件更容易。 然后,您可以在其他地方重用该逻辑。 不过,如果您没有使用c ++ 11甚至没有使用boost,那么进行必要的编译时函数可能会令人头疼。

如果使用复杂的(或任何简单的重新设计)可以满足您当前和将来的要求,请首选。 否则,我认为该解决方案可以合理地面向未来。

这可能会有所帮助。

#include<utility>

template<class Type>
struct test{
private:
    struct _matrix{};
    struct _scalar{};
    template<class T>
    struct category{
        typedef _scalar type;
    };
    template<class T>
    struct category<test<T>>{
        typedef _matrix type;
    };

    template<class T>
    void do_foo(T, _matrix){}

    template<class T>
    void do_foo(T, _scalar){}

public:
    //c++11
    template<class T>
    void operator*(T&& a){
        do_foo(std::forward<T>(a), category<T>::type());
    }
    //Older Compiler
    //template<class T>
    //void operator*(const T& a){
    //  do_foo(a, category<T>::type());
    //}

};

int main(){
    typedef test<int> int_matrix;
    int_matrix obj;
    obj*int_matrix();
    obj*obj;
    obj*1;
    obj*1.;

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM