Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: cannot erase const instance; bug or feature?

Reply
Thread Tools

Re: cannot erase const instance; bug or feature?

 
 
tom_usenet
Guest
Posts: n/a
 
      07-04-2003
On Fri, 4 Jul 2003 12:03:01 +0200, "Corno" <corno@%spam%.dds.nl>
wrote:

>Hi all,
>
>should the following code compile?
>
>void test(std::set<Thing *> things, Thing * const thing)
>{
> things.erase(thing);
>}
>
>My compiler complains that it cannot convert Thing * const to Thing *, but
>why should it need a non const pointer as this instance is not going to be
>changed?


That shouldn't cause a problem, since erase takes a const reference
argument anyway. Note that there's not much put in adding top level
const to function parameters, since they are copies anyway.

>As a work around, I now use a for loop to go through the set until I find
>the element and then I can delete that element.
>It seems an strange way to do it.


This code should compile fine:

#include <set>
class Thing;
void test(std::set<Thing *> things, Thing * const thing)
{
things.erase(thing);
}

If it doesn't, then your compiler is broken, otherwise you'll have to
post the code that *really* causes the error!

Tom
 
Reply With Quote
 
 
 
 
Corno
Guest
Posts: n/a
 
      07-05-2003

"tom_usenet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Fri, 4 Jul 2003 12:03:01 +0200, "Corno" <corno@%spam%.dds.nl>
> wrote:
>
> >Hi all,
> >
> >should the following code compile?
> >
> >void test(std::set<Thing *> things, Thing * const thing)
> >{
> > things.erase(thing);
> >}
> >
> >My compiler complains that it cannot convert Thing * const to Thing *,

but
> >why should it need a non const pointer as this instance is not going to

be
> >changed?

>
> That shouldn't cause a problem, since erase takes a const reference
> argument anyway. Note that there's not much put in adding top level
> const to function parameters, since they are copies anyway.
>
> >As a work around, I now use a for loop to go through the set until I find
> >the element and then I can delete that element.
> >It seems an strange way to do it.

>
> This code should compile fine:
>
> #include <set>
> class Thing;
> void test(std::set<Thing *> things, Thing * const thing)
> {
> things.erase(thing);
> }
>
> If it doesn't, then your compiler is broken, otherwise you'll have to
> post the code that *really* causes the error!
>

Aiii.... I placed the const in the wrong place. It should have been in
between the 'Thing' and the pointer (thus a pointer to a const Thing), see
below.
I've now tested it with 2 compilers (MS and G++) and they both give an
error.
So... it's a feature.
But what is the logic behind it?

#include <set>
class Thing;
void test(std::set<Thing *> things, Thing const * thing)
{
things.erase(thing);
}


Corno


 
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 vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
erase vs. erase al.cpwn@gmail.com C++ 7 03-30-2006 11:45 AM
How do i erase all information in my orkut and erase the link to the orkut account? mountbatten@gmail.com Computer Support 1 10-31-2005 12:03 AM
How do I erase a file that wont let me erase it? Dale Custer Computer Support 4 07-06-2003 09:48 AM



Advertisments