SFINAE and member template functions?

Hi,

can I test on the existance of member function templates using SFINAE? I tried and get compile errors with VC. Seems like it does not accept pointer to instantiated member function templates as template arguments..?

My "normal" class trait looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T, void (T::*)()> struct Empty {};

template<class T> float checkForFunction(...);
template<class T> char checkForFunction(Empty<T, &T::testFunction>*);
template<class T> struct Traits
{ enum { hasFunction = sizeof(checkForFunction<T>(0)) == 1 }; };

struct Foo { void testFunction() {} };
struct Bar {};

int main()
{
	cout << Traits<Foo>::hasFunction << endl;
	cout << Traits<Bar>::hasFunction << endl;
}
1
0


It works like a charm. However, when I use it to test for template member functions, it fails. Anyone knows why? Don't they have addresses too?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<class T, void (T::*)()> struct Empty {};

template<class T> float checkForFunction(...);
template<class T> char checkForFunction(Empty<T, &T::testFunction<int> >*);
template<class T> struct Traits
{ enum { hasFunction = sizeof(checkForFunction<T>(0)) == 1 }; };

struct Foo { template<class T> void testFunction() {} };
struct Bar {};

int main()
{
	cout << Traits<Foo>::hasFunction << endl;
	cout << Traits<Bar>::hasFunction << endl;

	cin.get();
}
0
0


Interestingly, when I comment the fallback ellipsis function, I get this error in line 6 (not line 4!)


1>  test.cpp
1>c:\users\imi\documents\visual studio 2010\projects\test\test\test.cpp(17): error C2770: invalid explicit template argument(s) for 'char checkForFunction(Empty<T,&T::testFunction<int>> *)'
1>          c:\users\imi\documents\visual studio 2010\projects\test\test\test.cpp(15) : see declaration of 'checkForFunction'
1>          c:\users\imi\documents\visual studio 2010\projects\test\test\test.cpp(24) : see reference to class template instantiation 'Traits<T>' being compiled
1>          with
1>          [
1>              T=Foo
1>          ]
1>c:\users\imi\documents\visual studio 2010\projects\test\test\test.cpp(17): error C2056: illegal expression


Any help? Anyone doing do compile-time checking for functions in another way?

Ciao, Imi.
SFINAE - one of those things that is on my tolearn list.
I just remembered.. Functions whose pointers are used as template argument must not have static linkage, right?

Do generated member function templates have static linkage? If so, can I change this somehow?

Ciao, Imi.
Topic archived. No new replies allowed.