4
#include <iostream>
#include <string>
using namespace std;
void printstr(const string & s) { cout << s << endl; }
template < typename A >
class Test
{
public:
typedef void (*Func)(const A &);
};
typedef void (*Func)(const string &);
template < typename A >
void bind(
Test<A>::Func f, //<---- does NOT compile
//Func f, //<---- compiles & works!
//void (*f)(const A &), //<---- compiles & works!
const A & a) { f(a); }
int main()
{
bind(printstr, string("test"));
return 0;
}
上記のコードでは、別のクラスの関数ポインタtypedefを使用しようとしています。示されているように、それはコンパイルされませんが、Test<A>::Func f,
行の代わりに他の2行のいずれかのコメントが外されていると、コンパイルは正常です。これは私がC++でできないものですか?どのような構文が必要ですか?テンプレートメンバー関数のtypedefはコンパイルされません
グラムの++ 4.4.3を使用して、私は名前Test<A>::Func
依存名前で、ヨハネスをチェックアウトする必要がありますより詳細な説明についてはtypename
typename Test<A>::Func f,
接頭辞する必要
test.cpp:20: error: variable or field "bind" declared void
test.cpp:20: error: expected ")" before "f"
test.cpp:23: error: expected primary-expression before "const"
[この回答](http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names/613132#613132)by Johannes to関連する質問は、 'typename'と従属名について知っておくべきことをすべて説明し、さらにもう少し説明します。 – sbi