[cfe-dev] Bug with pointer to const member function
Benoit Belley
Benoit.Belley at autodesk.com
Wed Oct 6 07:49:07 PDT 2010
Hi Everyone,
It seems that Clang fails to report an error when applying a pointer to a const member function to non-const reference or pointer as demonstrated by the following code example:
-----------------------------------------------------------------------------------------------------
class ClassA {};
typedef void (ClassA::*PtrToMem)();
typedef void (ClassA::*PtrToConstMem)() const;
void foo(
ClassA a,
const ClassA& a_const,
ClassA* ap,
const ClassA* ap_const,
PtrToMem ptr_to_mem,
PtrToConstMem ptr_to_const_mem
) {
(a.*ptr_to_mem)();
(a.*ptr_to_const_mem)();
(a_const.*ptr_to_mem)(); // expected-error{{invalid conversion from 'const ClassA*' to 'ClassA*'}}
(a_const.*ptr_to_const_mem)();
(ap->*ptr_to_mem)();
(ap->*ptr_to_const_mem)();
(ap_const->*ptr_to_mem)(); // expected-error{{invalid conversion from 'const ClassA*' to 'ClassA*'}}
(ap_const->*ptr_to_const_mem)();
}
-----------------------------------------------------------------------------------------------------
$ llvm/Debug+Asserts/bin/clang++ -c ptr_to_const_mem_bug.cpp
$
-----------------------------------------------------------------------------------------------------
Is this a known bug ?
I am currently using the SVN revision 114947 of clang.
Note that both the GNU g++ compiler and the Intel C++ compiler are properly reporting the errors:
-----------------------------------------------------------------------------------------------------
$ g++ -c ptr_to_const_mem_bug.cpp
ptr_to_const_mem_bug.cpp: In function 'void foo(ClassA, const ClassA&, ClassA*, const ClassA*, void (ClassA::*)(), void (ClassA::*)()const)':
ptr_to_const_mem_bug.cpp:18: error: invalid conversion from 'const ClassA*' to 'ClassA*'
ptr_to_const_mem_bug.cpp:24: error: invalid conversion from 'const ClassA*' to 'ClassA*’
$ icpc -c ptr_to_const_mem_bug.cpp
ptr_to_const_mem_bug.cpp(18): error: the object has cv-qualifiers that are not compatible with the member function
object type is: const ClassA
(a_const.*ptr_to_mem)(); // expected-error{{invalid conversion from 'const ClassA*' to 'ClassA*'}}
^
ptr_to_const_mem_bug.cpp(24): error: the object has cv-qualifiers that are not compatible with the member function
object type is: const ClassA
(ap_const->*ptr_to_mem)(); // expected-error{{invalid conversion from 'const ClassA*' to 'ClassA*'}}
^
compilation aborted for ptr_to_const_mem_bug.cpp (code 2)
$
-----------------------------------------------------------------------------------------------------
Cheers,
Benoit
Benoit Belley
Sr Principal Developer
M&E-Product Development Group
Autodesk Canada Inc.
10 Rue Duke
Montreal, Quebec H3C 2L7
Canada
More information about the cfe-dev
mailing list