重载运算符中的类型不匹配(写入管道)
Type mismatch in overloaded operator (writing pipelines)
只是测试/学习在重载|
运算符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; }
} // ()
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 重载运算符中的类型不匹配(写入管道)