Type of template member functions.

  • Thread starter Elias Salomão Helou Neto
  • Start date
E

Elias Salomão Helou Neto

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 reason I ask is because the following test always returns false:

template< class T >
class has_apply {

typedef char yes[1];
typedef char no[2];

template< class U, U u >
struct coerce {};

template< class U, unsigned n >
static yes& test( U*, coerce< void (U::*) ( const double& ) ,
&U::template apply< n > >* = 0 );

template< class U, unsigned n >
static no& test( ... );

public:

static const bool result = ( sizeof( yes ) == sizeof( test< T, 0
( (T*)(0) ) ) );
};
 
J

James Kanze

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 );
}


Is that a GCC bug or the types of template member function and
member functions are not the same?

You can't call it a bug, since the standard doesn't say what
typeinfo().name() should return. From a QoI point of view, of
course, an implementation outputs such random text is seriously
deficient.
The reason I ask is because the following test always returns false:
template< class T >
class has_apply {
typedef char yes[1];
typedef char no[2];
template< class U, U u >
struct coerce {};
template< class U, unsigned n >
static yes& test( U*, coerce< void (U::*) ( const double& ) ,
&U::template apply< n > >* = 0 );
template< class U, unsigned n >
static no& test( ... );

static const bool result = ( sizeof( yes ) == sizeof( test< T, 0
};

I don't see how it could do otherwise. You have one overload
of test which takes two or three arguments, another which can
take any number of arguments, and you call it with one.
Regardless of the types, the only match will be test(...).

I also don't see how this is related to typeid.
 
E

Elias Salomão Helou Neto

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?

You can't call it a bug, since the standard doesn't say what
typeinfo().name() should return.  From a QoI point of view, of
course, an implementation outputs such random text is seriously
deficient.








The reason I ask is because the following test always returns false:
template< class T >
class has_apply {
  typedef char yes[1];
  typedef char no[2];
  template< class U, U u >
  struct coerce {};
  template< class U, unsigned n >
  static yes& test( U*, coerce< void (U::*) ( const double& ) ,
&U::template apply< n > >* = 0 );
  template< class U, unsigned n >
  static no& test( ... );
public:
  static const bool result = ( sizeof( yes ) == sizeof( test< T, 0
( (T*)(0) ) ) );
};

I don't see how it could do otherwise.  You have one overload
of test which takes two or three arguments, another which can
take any number of arguments, and you call it with one.
Regardless of the types, the only match will be test(...).

I also don't see how this is related to typeid.

It takes one or two arguments: U* and coerce<>* = 0, take a look:

static yes& test( U*, coerce< void (U::*) ( const double& ),
&U::template apply< n > >* = 0 );

It would be related to typeid if a given type had a unique typeid,
because I thought

&A::member<1>

to be of type

void (A::*) ( const double& )

in which case my code would return true to has_apply< B >::result if B
were the following:

struct B {
template<unsigned n>
void apply( const double& );
};

since coerce<> would not fail to instantiate.
 
J

Juha Nieminen

James Kanze said:
From a QoI point of view, of
course, an implementation outputs such random text is seriously
deficient.

Does the sandard state that the intention of typeinfo().name() is to
return a human-readable string?
 
J

Johannes Schaub (litb)

Elias said:
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&)" - are
you sure that "member" in your code is not a static member function?
"PFvRKdE" is the mangled form for type "void (*)(double const&)".

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 case.
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 type
"void(const double&)", for the function type in your example.

The reason I ask is because the following test always returns false:

template< class T >
class has_apply {

typedef char yes[1];
typedef char no[2];

template< class U, U u >
struct coerce {};

template< class U, unsigned n >
static yes& test( U*, coerce< void (U::*) ( const double& ) ,
&U::template apply< n > >* = 0 );

template< class U, unsigned n >
static no& test( ... );

public:

static const bool result = ( sizeof( yes ) == sizeof( test< T, 0
( (T*)(0) ) ) );
};

As it appears you have a static member function, it's clear why this fails.
So double check everything.
 
E

Elias Salomão Helou Neto

Elias Salomão Helou Neto wrote:














The type of "&A::member<0>" is of type  "void(A::*)(const double&)" - are
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 case.

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 type
"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 said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ) const,
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( const double& ),
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ),
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ) const,
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( double ),
&U::template apply said:
);

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 fails.
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?
 
J

James Kanze

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?
You can't call it a bug, since the standard doesn't say what
typeinfo().name() should return. From a QoI point of view, of
course, an implementation outputs such random text is seriously
deficient.
The reason I ask is because the following test always returns false:
template< class T >
class has_apply {
typedef char yes[1];
typedef char no[2];
template< class U, U u >
struct coerce {};
template< class U, unsigned n >
static yes& test( U*, coerce< void (U::*) ( const double& ) ,
&U::template apply< n > >* = 0 );
template< class U, unsigned n >
static no& test( ... );
public:
static const bool result = ( sizeof( yes ) == sizeof( test< T, 0
( (T*)(0) ) ) );
};
I don't see how it could do otherwise. You have one overload
of test which takes two or three arguments, another which can
take any number of arguments, and you call it with one.
Regardless of the types, the only match will be test(...).
I also don't see how this is related to typeid.
It takes one or two arguments: U* and coerce<>* = 0, take a look:
static yes& test( U*, coerce< void (U::*) ( const double& ),
&U::template apply< n > >* = 0 );

Ah, yes. I got confused by that extra template in there. (But
I don't see where it is defined, so I can't say any more.)
It would be related to typeid if a given type had a unique typeid,
because I thought

to be of type
void (A::*) ( const double& )
in which case my code would return true to has_apply< B >::result if B
were the following:
struct B {
template<unsigned n>
void apply( const double& );

};
since coerce<> would not fail to instantiate.

I'm not sure I follow you. Sounds overly complicated to me.
 
J

James Kanze

Does the sandard state that the intention of typeinfo().name()
is to return a human-readable string?

The standard doesn't say anything about it; an implementation
which systematically returns "" is conform. That's why I spoke
of QoI; the only possible use for it is to display.
 
J

Johannes Schaub (litb)

Elias said:
Elias Salomão Helou Neto wrote:

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 type "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 said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ) const,
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( const double& ),
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ),
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( double ) const,
&U::template apply said:
);

template< class U, unsigned n >
static yes& test( U*,
binder< void (*) ( double ),
&U::template apply said:
);

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
fails. 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?

I tested your code and can't find any bug. Hmm, I suspect it's a GCC
problem.

For sure my assumption was wrong :)
 
R

Ruslan Mullakhmetov

Elias said:
Elias Salomão Helou Neto wrote:

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 type "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 said:
);

template< class U, unsigned n>
static yes& test( U*,
binder< void (U::*) ( const double& ) const,
&U::template apply said:
);

template< class U, unsigned n>
static yes& test( U*,
binder< void (*) ( const double& ),
&U::template apply said:
);

template< class U, unsigned n>
static yes& test( U*,
binder< void (U::*) ( double ),
&U::template apply said:
);

template< class U, unsigned n>
static yes& test( U*,
binder< void (U::*) ( double ) const,
&U::template apply said:
);

template< class U, unsigned n>
static yes& test( U*,
binder< void (*) ( double ),
&U::template apply said:
);

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
fails. 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?

I tested your code and can't find any bug. Hmm, I suspect it's a GCC
problem.

For sure my assumption was wrong :)

Visual Studio 2008 do not compile this code with error
has_apply<T>::test': function call missing argument list; use
'&has_apply<T>::test' to create a pointer to member

and actually i do not understand how it works. I do not understand
sizeof( test< T, 0u > > ( (T*)(0) ) )

why compare result of test (reference to yes or no) with null pointer
casted to T*?

please, give any clue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top