传递函数对象时出现编译器错误
Compiler error while passing function objects
假设您希望对容器对象的元素进行迭代修改,并在每次迭代时对元素进行处理。我想出的代码是这样做的:
#include <vector>
#include <iostream>
template <class InputIterator, class Operation, class Callback>
void transformAndTraverse(InputIterator begin, InputIterator end, Operation op, Callback cb) {
InputIterator start = begin;
while (begin != end) {
op(begin);
cb(start, end);
++begin;
}
}
template <class InputIterator>
struct plotFnObject {
void operator()(InputIterator begin, InputIterator end) {
while (begin < end) {
std::cout << *begin << " ";
++begin;
}
std::cout << std::endl;
}
};
template <class InputIterator>
struct addTwoFnObject {
void operator()(InputIterator idata) { *idata += 2; }
};
int main(int argc, char **argv) {
typedef std::vector<int>::iterator vec_iter;
std::vector<int> vec;
vec.push_back(0);
vec.push_back(0);
vec.push_back(0);
#ifndef DEMO_COMPILE_ERROR
// the below works
transformAndTraverse(vec.begin(), vec.end(), addTwoFnObject<vec_iter>(), plotFnObject<vec_iter>());
#else
// but this generates compilation errors
addTwoFnObject<vec_iter> addtwo();
plotFnObject<vec_iter> plot();
transformAndTraverse(vec.begin(), vec.end(), addtwo, plot);
#endif
}
问题是,我在第二种形式的中遇到了一些难以理解的编译器错误
$ g++ testtemplate.cpp -DDEMO_COMPILE_ERROR
testtemplate.cpp: In function 'void transformAndTraverse(InputIterator, InputIterator, Operation, Callback) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, Operation = addTwoFnObject<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > (*)(), Callback = plotFnObject<__gnu_cxx::__normal_iterator<int*, std::vector<int> > > (*)()]':
testtemplate.cpp:44:60: instantiated from here
testtemplate.cpp:8:5: error: too many arguments to function
testtemplate.cpp:9:5: error: too many arguments to function
$ g++ testtemplate.cpp
$
为什么编译器可以在线创建函数对象,而不能离线创建并传递它们?
$ g++ --version
g++ (GCC) 4.5.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这些是函数的声明:
addTwoFnObject<vec_iter> addtwo();
plotFnObject<vec_iter> plot();
这些是局部变量实例化:
addTwoFnObject<vec_iter> addtwo;
plotFnObject<vec_iter> plot;
您需要在末尾丢失多余的parens,否则编译器会抱怨您试图在没有提供参数列表的情况下调用假设函数addtwo
:
transformAndTraverse(vec.begin(), vec.end(), addtwo, plot);
addTwoFnObject<vec_iter> addtwo();
plotFnObject<vec_iter> plot();
声明了名为addtwo
和plot
的函数,分别返回addTwoFnObject<vec_iter>
和plotFnObject<vec_iter>
。卸下((。
谷歌搜索"最令人烦恼的解析"以获取更多信息。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误