Re: Is there a way to differentiate between the const version and non-const version of a member function ?
Helfer Thomas wrote:
Following my previous post called "Barton and nackman tricks : how to
enforce at compile time that a derived class has overloaded a given method
?", I have written the following piece of code :
namespace concept_check{
struct Test_is_same{
typedef char Small;
struct Big{
char dummy[2];
};
template<class A, class B>
static Small test(const A, const B);
template<class A>
static Big test(const A,const A);
};
}
#define METHOD_IS_OVERLOADED(x,y,z)
(sizeof(::concept_check::Test_is_same::test(&x::z,&y::z))==sizeof(::concept_check::Test_is_same::Small))
The macro METHOD_IS_OVERLOADED works has expected : it tells me if the
class y, implicitly a subclass of x, has overloaded the methode z.
The first error I encountered with its use is when I tried to overload
operator() which declared two types : one time "const" and one time "non
const". In that case, the macro METHOD_IS_OVERLOADED leads to a
compilation error : gcc complains about a "undefined type". It seems that
gcc can not differientiate between the two versions of operator(). Is
there a way to help gcc ?
Ordinarily a cast would be used to differentiate member pointers to
overloaded methods. And though a cast would resolve the compiler error
in this case, it would also render the overloaded test useless.
The overriden method test relies on the compiler deducing the member
pointer types on its own, and then checking whether the deduced types
for the member pointers differ - meaning that the subclass has
overriden the base method. But with the program specifying the
parameter types with casts, the compiler would no longer deduce their
types - so the subsequent comparison would simply compare the types
specified by the casts. And to cast appropriately, the program would
already have to know whether the method is overriden in the base class
- in other words for the test to work correctly for an overloaded
method, the program must already know what the correct result has to
be.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]