c++/8271: Templates and pointers to const member functions

René Møller Fonseca fonseca@mip.sdu.dk
Mon Jan 13 05:17:00 GMT 2003


http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8271

Hi,

It is GCC that is wrong. The qualifiers are part of the type and must not be ignored. Here is a more elaborate implementation.

>>>>>>
#include <iostream>

using namespace std;

class MyClass {
public:
  void mutableMethod() throw() {
    cout << __func__ << endl;
  }
  void constMethod() const throw() {
    cout << __func__ << endl;
  }
  void volatileMethod() volatile throw() {
    cout << __func__ << endl;
  }
  void constVolatileMethod() const volatile throw() {
    cout << __func__ << endl;
  }
};

MyClass* o = new MyClass();

template<class CLASS>
void mFunction(void (CLASS::* method)()) {
  (o->*method)();
}

template<class CLASS>
void cFunction(void (CLASS::* method)() const) {
  (o->*method)();
}

template<class CLASS>
void vFunction(void (CLASS::* method)() volatile) {
  (o->*method)();
}

template<class CLASS>
void cvFunction(void (CLASS::* method)() const volatile) {
  (o->*method)();
}

int main() {
  // cFunction(&MyClass::mutablemethod); // ERROR
  cFunction(&MyClass::constMethod); // OK
  mFunction(&MyClass::mutableMethod); // OK
  mFunction(&MyClass::constMethod); // ERROR - GCC bug
  // vFunction(&MyClass::volatileMethod); // ERROR
  vFunction(&MyClass::volatileMethod); // OK
  mFunction(&MyClass::constVolatileMethod); // ERROR - GCC bug
  cFunction(&MyClass::constVolatileMethod); // ERROR - GCC bug
  vFunction(&MyClass::constVolatileMethod); // ERROR - GCC bug
  cvFunction(&MyClass::constVolatileMethod); // OK
  return 0;
}
<<<<

The generated output (using GCC-3.2):
constMethod
mutableMethod
constMethod
volatileMethod
constVolatileMethod
constVolatileMethod
constVolatileMethod
constVolatileMethod

cheers,
René



More information about the Gcc-bugs mailing list