大きなクラスのプライベートデータ内のメンバークラスの関数ポインタであるパラメータをとるテンプレートクラスの関数を記述しようとしています。そのメンバーを呼び出すと、そのクラスを小さなクラスで呼び出します。実証する(右の混乱?)、私はここに非稼働の例があります。テンプレートクラスのテンプレート変数関数はコンパイルされません
#include <vector>
#include <iostream>
using namespace std;
template <typename T, typename C>
struct MyClass {
template <typename F, typename... A>
auto call_me(F func, A... args) { // pass in the function we want to call
return (mContainer.*func) (args...); // call the function supplied by
// the parameter on the private member data
}
C mContainer; // this will be private in my actual code
};
int main() {
MyClass<int, std::vector<int> > test;;
cout << test.call_me(&std::vector<int>::size) << endl; // works
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4); // doesn't work
return 0;
}
を、これは私の実際のコードが、私は何をしようとしているの小さな例ではないことに注意してください。ご覧のとおり、私はsize
のメンバー関数を 'Private'(私はデモのためにここに公開しています)vector
クラス内のMyClass
クラスのメンバー関数に呼び出そうとしています。私は解凍するようにコンパイラーにはパラメータを持っていないが、私は(解凍するためのパラメータを持っている)の挿入機能を実行しようとする場合、コンパイラは私のエラーを与えるたび、この唯一の作品:
.\template.cpp: In function 'int main()':
.\template.cpp:24:71: error: no matching function for call to 'MyClass<int, std::vector<int> >::call_me(<unresolved overloaded function type>, std::vector<int>::iterator, int)'
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4);
^
.\template.cpp:10:10: note: candidate: template<class F, class ... A> auto MyClass<T, C>::call_me(F, A ...) [with F = F; A = {A ...}; T = int; C = std::vector<int>]
auto call_me(F func, A... args) { // pass in the function we want to call
^~~~~~~
.\template.cpp:10:10: note: template argument deduction/substitution failed:
.\template.cpp:24:71: note: couldn't deduce template parameter 'F'
test.call_me(&std::vector<int>::insert, test.mContainer.begin(), 4);
これが同じですエラー私は実際の生産コードを取得しています。パラメータを指定しないで変数を指定して関数を呼び出して作業を展開しますが、それ以上のものを与えると同じエラーメッセージが表示されます。これはVariadicテンプレートを使用する私の最初の実際の試みです。だから、どんな勧告と助けにも感謝します。
は、どのように私はそれをやって行くのでしょうか?グーグルでは、static_castを使用するようになりますが、これはうまくいきません。 'test.call_me(static_cast :: iterator、int)>(&std :: vector :: insert)、test.mContainer.begin()、4);'を呼び出します。それを行う他の方法はありますか? –
Aryan
@Aryan私はちょうど答えにキャストを追加 – NathanOliver
ありがとう、それは働いた。もっと美しい方法がありますか?または、キーストロークの少ないもの? – Aryan