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

Type mismatch in overloaded operator (writing pipelines)

本文关键字:管道 不匹配 运算符 类型 重载      更新时间:2023-10-16

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

忽略了二进制表达式候选模板的无效操作数: ... 无法将"位集"与"矢量"匹配

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

// 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.

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

相反,接受函子的任何类型的内容:

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

然后找出返回类型。

返回类型是一个std::vector,其值类型是通过对 in 元素调用 f 返回的类型 - 换句话说,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;
    /*...*/
}

演示。

类似于

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;
}

编辑完成"实验性"代码。将运算符更改为==:变换、!=过滤和>>=减少/访问,因此避免在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; }
} // ()