Re: Type of template member functions.
On 8 nov, 20:39, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
Elias Salom=E3o Helou Neto wrote:
Hello, the following program when compiled with GCC gives the output
that follows the listing:
#include <iostream>
#include <typeinfo>
struct A {
template< unsigned n >
void member( const double& );
};
int main()
{
std::cout << typeid( void (A::*) ( const double& ) ).name() << '\n'=
;
std::cout << typeid( &A::member< 0 > ).name() << '\n';
std::cout << typeid( &A::member< 1 > ).name() << '\n';
return( 0 );
}
Output:
M1AFvRKdE
PFvRKdE
PFvRKdE
Is that a GCC bug or the types of template member function and member
functions are not the same?
The type of "&A::member<0>" is of type "void(A::*)(const double&)" - a=
re
you sure that "member" in your code is not a static member function?
"PFvRKdE" is the mangled form for type "void (*)(double const&)".
Yes, I have realized that GCC treats template _member_ function as
static functions, but this is not the point. Even if I try to bind to
an ordinary function the test still does not work, see below.
However your question implies that you think that "&A::member_function"
would be of the type of the member function, but that's not quite the cas=
e.
Could you explain why? I did not mean to imply that.
It will be a pointer to member. Only "A::member_function" - not preceeded=
by
& - will have the type of the member function, but that expression must
either be immediately followed by "()", or must unambiguously refer to a
static member function. In both cases, that expression would have the typ=
e
"void(const double&)", for the function type in your example.
Ok, now you've got the hole code:
#include <iostream>
template< class T >
class has_apply {
typedef char yes[1];
typedef char no[2];
template< class U, U u >
struct binder {};
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ) const,
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( const double& ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ) const,
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( double ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static no& test( ... );
public:
static const bool result = ( sizeof( yes ) == sizeof( test< T, 0u
( (T*)(0) ) ) );
};
class A {
public:
template< unsigned n >
void apply( const double& );
};
int main()
{
std::cout << std::boolalpha << has_apply< A >::result << '\n';
return( 0 );
}
Run:
$g++ -Wall -o test test.cpp&& ./test
false
As it appears you have a static member function, it's clear why this fail=
s.
So double check everything.
Your assumption is wrong, I do not have a static member function. Even
if I had, shouldn't the code above return true?