涉及自定义类的奇怪的模糊重载

时间:2017-03-25 18:51:05

标签: c++

我的编译器告诉我有一个模糊的重载。我无法理解为什么会这么想。

我在example.h中有以下库(请一直向下滚动):

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>

template <class T> class Matrix
{

  private:

    std::vector<T>      _data;
    std::vector<size_t> _shape;

  public:

    Matrix               (const Matrix<T> &) = default;
    Matrix<T>& operator= (const Matrix<T> &) = default;
    Matrix<T>            (                 ) {};

    Matrix<T>( std::vector<size_t> shape )
    {
      size_t n = 1;

      for ( size_t i=0 ; i<shape.size() ; i++ ) {
        _shape.push_back(shape[i]);
        n *= shape[i];
      }

      while ( _data.size()<n )
        _data.push_back((T)0);
    };

};

std::tuple<Matrix<double>,int> S2 (\
  Matrix<int> & in1, Matrix<int> &in2, std::vector<size_t> &shape \
)
{
  Matrix<double> out(shape);
  return std::make_tuple(out,10);
};

std::tuple<Matrix<double>,Matrix<int>> S2 (\
  Matrix<int> & in1, Matrix<int> & in2, std::vector<size_t> & shape,\
  bool a=false, bool b=false\
)
{
  Matrix<double> out (shape);
  Matrix<int>    out2(shape);
  return std::make_tuple(out,out2);
};

#endif

从以下文件调用:

#include "example.h"

int main ( void )
{

  std::vector<size_t> shape;
  shape.push_back(11);
  shape.push_back(11);

  int n;
  Matrix<int>    in1({100,100});
  Matrix<int>    in2({100,100});
  Matrix<double> out;

  std::tie(out,n) = S2 ( in1, in2 , shape );

  return  0;
}

使用clang++ -std=c++14 example.cpp

编译

1 个答案:

答案 0 :(得分:0)

bool参数的默认值使函数不明确, 因为可以使用参数(Matrix&amp;,Matrix&amp;,std :: vector&amp;)调用这两个函数。