渡された関数が暗黙的にstd::function
にキャストされるように、反復可能関数と関数を受け入れるテンプレート関数を作成しようとしています。 (したがって、完全な関数とラムダの両方で使用することができます)。(g ++ 4.7.1)明示的な型名を同等のクラスtypedefに置き換えることはできません
は、ここでは、コードです:g++-4.7 -std=c++11 -o itest itest.cpp
でコンパイル
#include <iostream>
#include <vector>
#include <algorithm>
#include <typeinfo>
template<typename T>
void bar(const T & base, std::function<bool(int)> f) // works
//void bar(const T & base, std::function<bool(typename T::iterator::value_type)> f) // fails to compile
{
std::cout << ((typeid(std::function<bool(int)>) == typeid(std::function<bool(typename T::iterator::value_type)>))?"identical":"distinct") << std::endl;
}
bool filter(int x) { return x%2==0; }
int main() { bar(std::vector<int> {0, 1}, filter); }
これはidentical
を生成します。
代わりにコンパイルが
g++-4.7 -std=c++11 -Wall -Werror -o itest itest.cpp
itest.cpp: In function 'int main()':
itest.cpp:16:53: error: no matching function for call to 'bar(std::vector<int>, bool (&)(int))'
itest.cpp:16:53: note: candidate is:
itest.cpp:9:10: note: template<class T> void bar(const T&, std::function<bool(typename T::iterator::value_type)>)
itest.cpp:9:10: note: template argument deduction/substitution failed:
itest.cpp:16:53: note: mismatched types 'std::function<bool(typename T::iterator::value_type)>' and 'bool (*)(int)'
で失敗し、あなたのコメントを解除ライン10とコメント行9場合は、上記のようにコンパイルし、私は(適切なオプションを設定した)無修正版はXcodeのに成功したことに注意する必要がありますが、私は」可能であれば、g ++に固執することを好みます。何か間違っているのですか、これはg ++の既知のバグですか?
ありがとう、ありがとう。私は別の(むしろ恐ろしい)回避策を実装しました.1つは、呼び出し側が第2の型を参照または導出する必要がない方法です。 – Bakkot
おっと、[リンク](https://gist.github.com/a172651b93dc4670f295)を忘れました。両方を読んで欲しくない人にとって、それは本質的にカレーです。 – Bakkot