boost::运算符混合运算

boost::operators mixed arithmetic

本文关键字:运算 混合 运算符 boost      更新时间:2023-10-16

基于这里的示例http://www.boost.org/doc/libs/release/libs/utility/operators.htm#example,我已经实现了boost::numeric::ublas::vector的以下派生类:

namespace Chebyshev
{
  template<typename T>
  class function_data : public boost::numeric::ublas::vector<T>,
                               boost::addable<function_data<T> >,
                               boost::subtractable<function_data<T> >,
                               boost::multipliable2<function_data<T>, T>,
                               boost::dividable2<function_data<T>, T>
  {
    public:
      char dataflag;
      function_data() : boost::numeric::ublas::vector<T>() {dataflag=0;} ///< The default empty constructor
      function_data(const boost::numeric::ublas::vector<T>& vec) : boost::numeric::ublas::vector<T>(vec) {dataflag=0;} ///< The copy constructor without a flag.
      function_data(const boost::numeric::ublas::vector<T>& vec, char flag) : boost::numeric::ublas::vector<T>(vec), dataflag(flag) {} ///< The copy constructor with a flag.
      ~function_data() {} ///< The destructor.
      function_data<T>& operator= (const boost::numeric::ublas::vector<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); return *this;} ///< The assignment operator from a boost::numeric::ublas::vector<T>.
      function_data<T>& operator= (const function_data<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); dataflag=in.dataflag; return *this;} ///< The assignment operator.
      function_data<T>& operator+= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator+=(in); this->dataflag=this->dataflag; return *this;}
      function_data<T>& operator-= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator-=(in); this->dataflag=this->dataflag; return *this;}
      function_data<T>& operator*= (T in) {this->boost::numeric::ublas::vector<T>::operator*=(in); this->dataflag=this->dataflag; return *this;}
      function_data<T>& operator/= (T in) {this->boost::numeric::ublas::vector<T>::operator/=(in); this->dataflag=this->dataflag; return *this;}
      friend std::ostream& operator<< (std::ostream& os, const function_data<T>& fd) {os << "[type " << fd.dataflag << "] " << static_cast<boost::numeric::ublas::vector<T> >(fd); return os;} ///< The << operator.
  };
}

然而,编译以下代码片段

int main( int argc, char ** argv)
{
  Chebyshev::function_data<std::complex<double> > u;
  /* some stuff putting values in u */
  std::cout << u*2 << std::endl;
  return 0;
}

给出一个"ISO C++说这些都是模糊的,即使第一个的最差转换比第二个的最坏转换好"警告,并继续给出ublas vector_expression版本(u转换为某种vector_eexpression)和我的版本(2转换为const std::complex<double>&)。

我希望能够像上面的代码片段一样在课堂上使用混合算术,但boost::operators网站上的解释我不清楚。我必须在课堂上添加或更改什么才能实现这一点?

此外,在本例中,继承列表中的每个类都位于前一个类的最后一个>中。无论我是用这种方式还是用上面的方式写,我都看不出编译器的输出有什么不同。哪种写法正确?

致以最良好的问候,布雷特。

Boost uBLAS具有矢量运算所需的所有运算符。以下是一个突出显示它的示例程序:

#include <iostream>
#include "boost/numeric/ublas/vector.hpp"
#include "boost/numeric/ublas/io.hpp"
using namespace boost::numeric::ublas;
int main()
{
  vector<int> v1(4);
  vector<int> v2(4);
  vector<int> v3(4);
  vector< std::complex<double> > v4(4);
  for (size_t i = 0; i < v1.size(); i++)
    v1(i) = (i + 1) * 3;
  for (size_t i = 0; i < v1.size(); i++)
    v2(i) = (i + 1) * 10;
  for (size_t i = 0; i < v4.size(); i++)
    v4(i) = std::complex<double>(v1(i), v2(i));
  std::cout << "v1: " << v1 << std::endl;
  std::cout << "v2: " << v2 << std::endl;
  std::cout << "v3 = v2 * 3: " << (v3 = v2 * 3) << std::endl;
  std::cout << "v4: " << v4 << std::endl;
  std::cout << "v1 + v2: " << v1 + v2 << std::endl;
  std::cout << "v1 - v2: " << v1 - v2 << std::endl;
  std::cout << "v1 * 3: " << v1 * 3 << std::endl;
  std::cout << "(v2 * 2) / 3: " << (v2 * 2) / 3 << std::endl;
  std::cout << "v4 * 3: " << v4 * 3 << std::endl;
  std::cout << "v4 + v4" << v4 + v4 << std::endl;
  std::cout << "element_prod(v1, v2)" << element_prod(v1, v2) << std::endl;
  std::cout << "element_div(v2, v1)" << element_div(v2, v1) << std::endl;
  return 0;
}

以下是我使用g++v4.1.2:编译和运行时得到的输出

[ublas]$ g++ -o vector vector.cpp 
[ublas]$ ./vector 
v1: [4](3,6,9,12)
v2: [4](10,20,30,40)
v3 = v2 * 3: [4](30,60,90,120)
v4: [4]((3,10),(6,20),(9,30),(12,40))
v1 + v2: [4](13,26,39,52)
v1 - v2: [4](-7,-14,-21,-28)
v1 * 3: [4](9,18,27,36)
(v2 * 2) / 3: [4](6,13,20,26)
v4 * 3: [4]((9,30),(18,60),(27,90),(36,120))
v4 + v4[4]((6,20),(12,40),(18,60),(24,80))
element_prod(v1, v2)[4](30,120,270,480)
element_div(v2, v1)[4](3,3,3,3)