Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const/non const member function vs. templates

Reply
Thread Tools

const/non const member function vs. templates

 
 
Stuart Redmann
Guest
Posts: n/a
 
      07-28-2011
On 26 Jul., Markus Keppeler wrote:
> Hi Together!
>
> In some (partly legacy) code I have member functions like this:
>
> // class Dummy (class of any type).
> class XY
> {
> Dummy *pGetMe( (returns pointer to some
> ); dummy member orwhatever)
>
> const Dummy *pGetMe( (returns const pointer to some
> ) const; dummy member or whatever).
>
> }
>
> Since the implementations on both pGetMe's are 100% identically, only
> that they use const/non const types/functions, I'd like to merge them.
>
> I cannot implement the non-const version by calling down to the const
> one, since at some level it needs to fetch the non-const pointer to
> something. Here also some legacy-issues come into, so I cannot
> refactor the entire design .
>
> My next thought was to use templates, like
>
> template <class T>
> T pGetMe();
>
> , but here I don't know how to deal with the const/non constness of
> the member function. Can I add the const/non const based on the type
> of T?


The following template should do what you want, but the syntax is a
bit different: You'd have to declare a public member Me and take the
address of it:
class XY
{
public:
ConstCorrectAccessor<Dummy> Me;
};

void foo (const XY& xy)
{
const Dummy* OK = &xy.Me;
Dummy* DOES_NOT_COMPILE = &xy.Me;
}

--------------------------------------------------------------------------------------------------------------------------------------------
// Wrapper for plain pointers that behaves as const-correct
// accessor.
template<class t_Class>
class ConstCorrectAccessor
{
t_Class* m_InternalPointer;
public:
ConstCorrectAccessor (t_Class* Pointer)
: m_InternalPointer (Pointer)
{}

// Accessor methods with const-correct overload.
const t_Class* operator-> () const {return m_InternalPointer;}
t_Class* operator ->() {return m_InternalPointer;}

const t_Class* operator& () const {return m_InternalPointer;}
t_Class* operator& () {return m_InternalPointer;}
};

class SomeClass
{
public:
void foo () const {}
void bar () {}
};

class AnotherClass
{
public:
ConstCorrectAccessor<SomeClass> ConstCorrectAccess;
SomeClass* PlainPointerAccess;
public:
AnotherClass (SomeClass* Object)
: PlainPointerAccess (Object),
ConstCorrectAccess (Object)
{}

void test () const
{
ConstCorrectAccess->foo (); // OK
//ConstCorrectAccess->bar (); // Error: Non-const method on
SomeObject.
PlainPointerAccess->foo (); // OK
PlainPointerAccess->bar (); // BAD: Const-correctness not
propagated.
}
};


int main ()
{
SomeClass a;
const AnotherClass b (&a);
b.ConstCorrectAccess->foo (); // OK
// b.ConstCorrectAccess->bar (); // Compilation error: b is const
b.PlainPointerAccess->foo (); // OK
b.PlainPointerAccess->bar (); // BAD: Const-correctness is not
propagated

AnotherClass c (&a);
c.ConstCorrectAccess->foo (); // OK
c.ConstCorrectAccess->bar (); // OK: c is not const
c.PlainPointerAccess->foo (); // OK
c.PlainPointerAccess->bar (); // OK: c is not const

const SomeClass* constpointer2SomeClass = &b.ConstCorrectAccess;
const SomeClass* constpointer2SomeClass2 = &c.ConstCorrectAccess;
// SomeClass* pointer2SomeClass = &b.ConstCorrectAccess; //
Compilation error: b is const
SomeClass* pointer2SomeClass2 = &c.ConstCorrectAccess;

return 0;
}


Regards,
Stuart
 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      07-29-2011
On 26 Jul., 14:58, Markus Keppeler <(E-Mail Removed)> wrote:
> Hi Together!
>
> In some (partly legacy) code I have member functions like this:
>
> // class Dummy * * (class of any type).
> class XY
> {
> * *Dummy *pGetMe( * * * * * *(returns pointer to some
> * *); * * * * * * * * * * * *dummy member orwhatever)
>
> * *const Dummy *pGetMe( * * *(returns const pointer to some
> * *) const; * * * * * * * * *dummy member or whatever).
>
> }
>
> Since the implementations on both pGetMe's are 100% identically, only
> that they use const/non const types/functions, I'd like to merge them.
>
> I cannot implement the non-const version by calling down to the const
> one, since at some level it needs to fetch the non-const pointer to
> something. Here also some legacy-issues come into, so I cannot
> refactor the entire design .
>
> My next thought was to use templates, like
>
> * *template <class T>
> * *T pGetMe();
>
> , but here I don't know how to deal with the const/non constness of
> the member function. Can I add the const/non const based on the type
> of T?
>


The following is not exactly what you want but it comes quite close:

// Wrapper for plain pointers that behaves as const-correct
// accessor.
template<class t_Class>
class ConstCorrectAccessor
{
t_Class* m_InternalPointer;
public:
ConstCorrectAccessor (t_Class* Pointer)
: m_InternalPointer (Pointer)
{}

// Accessor methods with const-correct overload.
const t_Class* operator-> () const {return m_InternalPointer;}
t_Class* operator ->() {return m_InternalPointer;}

const t_Class* operator& () const {return m_InternalPointer;}
t_Class* operator& () {return m_InternalPointer;}
};

class Dummy;

class XY
{
public:
ConstCorrectAccessor<Dummy> Me;
};

void foo (const XY& xy)
{
const Dummy* OK = &xy.Me;
Dummy* DOES_NOT_COMPILE = &xy.Me;
}
 
Reply With Quote
 
 
 
 
Markus Keppeler
Guest
Posts: n/a
 
      07-29-2011
Am 28.07.2011 15:11, schrieb Stuart Redmann:
> // Accessor methods with const-correct overload.
> const t_Class* operator-> () const {return m_InternalPointer;}
> t_Class* operator ->() {return m_InternalPointer;}
>
> const t_Class* operator& () const {return m_InternalPointer;}
> t_Class* operator& () {return m_InternalPointer;}


Damn, thats quite an interesting way to solve this, although it took
me a minute to understand .

This changes the design etc. of the class, and is a little verbose, so
I'll stick with the const/non const duplicates as long as the
functions are small.

I fear there is not such a beauty solution as I hoped (on the other
hand, I'm glad that I didn't miss any obvious thing ).

Thanks for input!
Markus

--
Nur tote Fische schwimmen mit dem Strom.
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      07-29-2011
On 29 Jul., Stuart Redmann wrote:

[snip my own posting ]

Please ignore this posting and the posting it is refering to. Somehow
either Google Groups or my proxy seems to be a bit sluggish as I
haven't seen my posting until 10 minutes ago.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Overriding const member function with non-const David Scarlett C++ 3 02-07-2006 01:34 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments