重载运算符中的类型不匹配(写入管道)

时间:2015-01-20 14:58:13

标签: c++ c++11

只是测试/学习在C ++中编写管道,使|运算符重载,以下程序 无法编译:

  

忽略二进制表达式候选模板的无效操作数:   ...   无法匹配' bitset'反对' vector'

似乎编译器正在尝试使用标准|定义。 该代码使用显式调用和显式类型参数设置。

// g++ -std=c++11 Pipeline.cpp 
#include <iostream>
#include <vector>

// ..............................................................
// ..............................................................
std::string dup (std::string s) {
  return s + s;
}

// ..............................................................
// ..............................................................
template <typename TI, typename TO>
std::vector<TO> operator | (const std::vector<TI> & in, std::function<TO(TI)> f) {
  std::vector<TO> out;
  for (auto i : in) {
    out.push_back ( f(i) );
  }
  return out;
}

// ..............................................................
// ..............................................................
int main () {
  std::cout << " hello " << std::endl;

  std::vector<std::string> vs = {"one", "two", "three", "four", "five"};

  auto res = vs | dup;
  // OK: vector<string> res = operator|<string,string> (vs, dup);

  for (auto s : res) { std::cout << s << std::endl; }

} // ()

完整的错误消息:

Pipeline.cpp:29:17: error: invalid operands to binary expression
      ('std::vector<std::string>' and 'std::string (*)(std::string)')
  auto res = vs | dup;
             ~~ ^ ~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/bitset:1045:1: note: 
      candidate template ignored: could not match 'bitset' against 'vector'
operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
^
Pipeline.cpp:13:17: note: candidate template ignored: could not match
      'function<type-parameter-0-1 (type-parameter-0-0)>' against
      'std::__1::basic_string<char> (*)(std::__1::basic_string<char>)'
std::vector<TO> operator | (const std::vector<TI> & in, std::function<TO...
                ^
1 error generated.

2 个答案:

答案 0 :(得分:6)

模板参数推导不会查看隐式转换。函数指针不是std::function,因此编译器无法推断出用operator|实例化的模板参数。通常,std::function<...>,其中...包含模板参数,不是接受任意函数或函数对象的正确方法。

相反,接受仿函数的任何类型:

template <typename TI, typename F>
/* something */ operator | (const std::vector<TI> & in, F f) 

然后找出返回类型。

返回类型是std::vector,其值类型是通过在f元素上调用in返回的类型 - 换句话说,decltype(f(in[0]))。但是,f可以返回引用类型,并且您确实希望在这些情况下向量的值类型是引用的类型。使用std::decay(或std::remove_reference)去除任何参考资料:

template <typename TI, typename F>
auto operator | (const std::vector<TI> & in, F f) -> std::vector<typename std::decay<decltype(f(in[0]))>::type> {
    std::vector<typename std::decay<decltype(f(in[0]))>::type> out;
    /*...*/
}

Demo

答案 1 :(得分:0)

T.C。

的类似解决方案
template <typename CONT, typename F>  // types: container , function
auto operator | (const CONT & a, F f) {
  // find out input type and output type
  using TI = typename CONT::value_type;
  TI aux;
  using TO = decltype(f(aux));
  // do the task
  vector<TO> res;
  for (auto & i : a) {
    res.push_back( f(i) );
  }
  return res;
}

修改 完成&#34;实验&#34;码。将运营商更改为 因此,==:转换,!=过滤器和>>=缩减/访问 避免在像vs == dup != lengthy != contains == length >>= add;这样的表达式中需要括号(我们需要变换和文件管理器具有相同的优先级,并且reduce更少)。不知道在使用它们的其他表达方式中是否会产生附带问题。

// g++ -std=c++1y Pipeline.cpp
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// ..............................................................
// transform
// ..............................................................
template <typename CONT, typename F>
auto operator == (const CONT & a, F  f) {
  using IT = typename CONT::value_type;
  IT aux;
  using OT = decltype(f(aux));
  vector<OT> res;
  for (auto & i : a) {
    res.push_back( f(i) );
  }
  return res;
}

// ..............................................................
// filter
// ..............................................................
template <typename CONT, typename F>
auto operator != (const CONT & a, F  f) {
  using IT = typename CONT::value_type;
  IT aux;
  vector<IT> res;
  for (auto & i : a) {
    if (f(i)) res.push_back( i );
  }
  return res;
}

// ..............................................................
// reduce / visit
// ..............................................................
template <typename CONT, typename F>
auto operator >>= (const CONT & a, F  f) {
  for (auto & i : a) {
    f(i);
  }
  return f;
}

// ..............................................................
// ..............................................................
int main () {
  vector<string> vs = {"one", "two", "three", "four", "five"};

  int sum = 0;

  auto dup = [] (const string &s) -> string { return s+s; };
  auto lengthy = [] (const string &s) -> bool { return s.length()>6; };
  auto contains = [] (const string &s) -> bool { return s.find('f') != string::npos; };

  auto length = [] (const string &s)   { return s.length(); };
  auto add = [&sum] (int i) { sum += i;};

  // == transform
  // != filter
  // >>= reduce / visit
  vs == dup != lengthy  != contains == length >>= add;

  cout << sum << endl;

  auto res1 =  vs == dup != lengthy  != contains;

  for (auto & s : res1) { cout << s << endl; }

} // ()