Re: cannot erase const instance; bug or feature?

Discussion in 'C++' started by tom_usenet, Jul 4, 2003.

  1. tom_usenet

    tom_usenet Guest

    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
    tom_usenet, Jul 4, 2003
    #1
    1. Advertising

  2. tom_usenet

    Corno Guest

    "tom_usenet" <> wrote in message
    news:...
    > 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
    Corno, Jul 5, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    354
    Pete Becker
    Mar 30, 2006
  2. Replies:
    11
    Views:
    1,089
  3. Javier
    Replies:
    2
    Views:
    548
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,095
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    873
    fungus
    Oct 31, 2008
Loading...

Share This Page