Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #3792
| From | Kai-Uwe Bux <jkherciueh@gmx.net> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: const-correctness loopholes |
| Followup-To | comp.lang.c++ |
| Date | 2011-04-15 22:15 +0200 |
| Organization | University of Bielefeld |
| Message-ID | <ioa91t$m5e$1@hoshi.visyn.net> (permalink) |
| References | <4da89d59$0$38645$4fafbaef@reader1.news.tin.it> |
Followups directed to: comp.lang.c++
noir wrote:
> Hello,
> I was reading about const-correctness on wikipedia:
> http://en.wikipedia.org/wiki/Const-correctness
>
> and things mostly made sense until I got to the part where "shallow"
> const-correctness is discussed, and a solution is presented (i.e.
> wrapping the struct/class with a const-correct interface):
>
> --- quote ---
> The latter loophole can be closed by using a class to hide the pointer
> behind a const-correct interface, but such classes either don't support
> the usual copy semantics from a const object (implying that the
> containing class cannot be copied by the usual semantics either) or
> allow other loopholes by permitting the stripping of const-ness through
> inadvertent or intentional copying.
> --- quote ---
>
> So it looks like there's a dark side to this solution, but... what is
> it?! I cannot make it out from the above snippet.
Here is an example in code:
template < typename T >
class const_correct_ptr {
typedef T* pointer;
typedef T& reference;
typedef T const * const_pointer;
typedef T const & const_reference;
pointer the_ptr;
public:
const_correct_ptr ( pointer ptr = 0 )
: the_ptr ( ptr )
{}
pointer operator-> ( void ) {
return ( the_ptr );
}
const_pointer operator-> ( void ) const {
return ( the_ptr );
}
reference operator* ( void ) {
return ( *the_ptr );
}
const_reference operator* ( void ) const {
return ( *the_ptr );
}
};
typedef const_correct_ptr< int > int_ptr;
struct example {
int_ptr p;
example ( int * ptr )
: p ( ptr )
{}
};
#include <iostream>
#include <ostream>
int main ( void ) {
{
int i = 5;
example x ( &i );
*x.p = 6;
std::cout << i << "\n";
}
{
int i = 5;
example const x ( &i );
// *x.p = 6; // fails to compile! deep const
}
{
int i = 5;
example const x ( &i );
int_ptr p ( x.p ); // initializing from x, which is const!
*p = 6;
std::cout << i << "\n";
}
}
The first block in main shows that to some extend, x.p behaves like a
pointer member of.
The second block shows that this pointer member inherits constness from the
object x. So it seems to close the loophole of constness.
The third block shows how a simple copy construction brings back to life
write access to the pointee. Now, you face a dilemma: either you could
suppress ordinary copy construction from const& or you leave this kind of
hole.
What this goes to show is that exposed pointer members are tricky and you
have to be aware of how they behave.
Best,
Kai-Uwe Bux
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
const-correctness loopholes noir <none@none.no> - 2011-04-15 21:32 +0200
Re: const-correctness loopholes Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-15 15:13 -0500
Re: const-correctness loopholes noir <none@none.no> - 2011-04-16 11:29 +0200
Re: const-correctness loopholes Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-15 22:15 +0200
Re: const-correctness loopholes noir <none@none.no> - 2011-04-16 11:29 +0200
Re: const-correctness loopholes noir <none@none.no> - 2011-04-16 11:36 +0200
Re: const-correctness loopholes Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-04-16 12:21 +0200
Re: const-correctness loopholes noir <none@none.no> - 2011-04-16 12:44 +0200
csiph-web