首页 > 解决方案 > 在 C++ 11 中使用 decltype 和模板进行矩阵加法

问题描述

在 The C++ Programming Language 的第四版中,Stroustrup 博士用一个例子来计算两个矩阵的结果来演示 decltype() 说明符。我的问题是第一部分括号后的箭头运算符 -> 之间的关系。声明的结果是此代码中的引用吗?(以 auto operator+(const ...) 开头的部分

template<typename T, typename U>
auto operator+(const Matrix<T>& a, const Matrix<U>& b)-> Matrix<decltype(T{}+U{})>
{
  Matrix<decltype(T{}+U{})> rest;
  for (int i=0; i!=a.rows(); ++i)
       for (int j=0; j!=a.cols(); ++j)
            res(i,j) += a(i,j) + b(i,j);
  return res;
}

标签: c++templatesmatrixautodecltype

解决方案


推荐阅读