【发布时间】:2021-07-12 17:08:16
【问题描述】:
我想写一个函数,它可以接收一个ostream、一个可迭代对象v和一个函数op。该函数应该对 v 中的每个元素调用 op,然后将结果发送到 ostream。所以我写了以下代码:
template<typename T,
typename IN = decltype(*std::declval<T>().begin()),
typename FT = function<ostream&(ostream&, const IN&)>
>
void func(ostream& ss, const T& v,
FT op = [](ostream& os, const IN&v)->ostream&{ return os << v; }) {
for (const auto& i: v) {
std::invoke(op, ss, i);
}
}
int main() {
vector<int> vec = {1, 2, 3, 4};
func(cout, vec);
return 0;
}
当我尝试使用 clang++ 编译这些代码时,出现以下错误。
/mnt/d/codes/stlprinter/stack.cpp:42:9: error: no matching function for call to 'invoke'
std::invoke(op, ss, i);
^~~~~~~~~~~
/mnt/d/codes/stlprinter/stack.cpp:48:5: note: in instantiation of function template specialization 'func<std::vector<int, std::allocator<int>>, int &, std::function<std::basic_ostream<char> &(std::basic_ostream<char> &, int &)>>' requested here
func(cout, vec);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/functional:85:5: note: candidate template ignored: substitution failure [with _Callable = std::function<std::basic_ostream<char> &(std::basic_ostream<char> &, int &)> &, _Args = <std::basic_ostream<char> &, const int &>]: no type named 'type' in 'std::invoke_result<std::function<std::basic_ostream<char> &(std::basic_ostream<char> &, int &)> &, std::basic_ostream<char> &, const int &>'
invoke(_Callable&& __fn, _Args&&... __args)
^
1 error generated.
错误信息表明在调用invoke时,op的类型是_Callable = std::functionstd::basic_ostream
【问题讨论】:
标签: c++ c++11 templates functional-programming