簡単なことです。しかし、私は最後の 1 時間を費やし、理解できませんでした。
次のコードをコンパイルすると:
#include <iostream>
#include <sort.h>
#define array_len(arr) ( sizeof(arr) / sizeof (*arr) )
using namespace std;
template<typename ITER>
void printIt_works(ITER b, ITER e) {
for_each(b, e, [](int it) { cout << it; } ); // putting int explicitly would work
// but it's not generic
}
template<typename ITER>
void printIt_doesnt_work(ITER b, ITER e) {
for_each(b, e, [](ITER it) { cout << *it; } );
}
int main() {
int a[] = {5, 2, 4, 6, 1, 3};
printIt_doesnt_work(a, a+array_len(a)); // how to make this work in a generic way.
//merge_sort(a, a+array_len(a));
selection_sort(a, 6);
insertion_sort_decending(a, 6);
insertion_sort(a, 6);
return 0;
}
私が得るコンパイルエラーは次のとおりです。
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/algorithm:63:0,
from D:\Workspaces\CodeBlocks\Test\main.cpp:4:
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = printIt_doesnt_work(ITER, ITER) [with ITER = int*]::<lambda(int*)>]':
D:\Workspaces\CodeBlocks\Test\main.cpp:17:5: instantiated from 'void printIt_doesnt_work(ITER, ITER) [with ITER = int*]'
D:\Workspaces\CodeBlocks\Test\main.cpp:23:42: instantiated from here
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185:2: error: invalid conversion from 'int' to 'int*'
d:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185:2: error: initializing argument 1 of 'printIt_doesnt_work(ITER, ITER) [with ITER = int*]::<lambda(int*)>'
d:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algo.h:4185 は、for_each
3 番目のパラメーターとして渡された関数を呼び出す場所です。__f(*__first);
私は問題を理解しています。私のラムダ関数は期待するように宣言されてint*
いますが、のテンプレートインスタンスfor_each
はint
. 私はそれをある意味で解決する方法を知りませんgeneric
。
もちろん、型を明示的にすることで回避できますが、それは一般的ではありません。
for_each(b, e, [](int it) { cout << it; } );