Casting away constness

Discussion in 'C++' started by Martin Magnusson, Nov 17, 2003.

  1. I have a typedef which looks like this:

    typedef std::list< const S* const > seq_type;

    I also have a class with a private field:

    seq_type m_Sequence;

    When calling the following method

    M::AddSequence( seq_type seq )
    {
    m_Sequence.insert( m_Sequence.begin(),
    seq.begin(),
    seq.end() );
    }

    which is supposed to prepend the list "seq" to "m_Sequence" gcc tells me
    that:

    /usr/include/c++/3.2/bits/stl_construct.h:78: static_cast from type
    `const S* const*' to type `void*' casts away constness

    Why is that?

    / martin
     
    Martin Magnusson, Nov 17, 2003
    #1
    1. Advertising

  2. Martin Magnusson

    tom_usenet Guest

    On Mon, 17 Nov 2003 11:26:34 +0100, Martin Magnusson
    <> wrote:

    >I have a typedef which looks like this:
    >
    >typedef std::list< const S* const > seq_type;


    The above is illegal - the elements of a list must be assignable. So:

    typedef std::list< const S* > seq_type;

    would be allowed. Some implementations might allow the former, but it
    isn't standard or portable.

    >
    >I also have a class with a private field:
    >
    >seq_type m_Sequence;
    >
    >When calling the following method
    >
    >M::AddSequence( seq_type seq )
    >{
    > m_Sequence.insert( m_Sequence.begin(),
    > seq.begin(),
    > seq.end() );
    >}
    >
    >which is supposed to prepend the list "seq" to "m_Sequence" gcc tells me
    >that:
    >
    >/usr/include/c++/3.2/bits/stl_construct.h:78: static_cast from type
    >`const S* const*' to type `void*' casts away constness
    >
    >Why is that?


    I suspect for the reason I listed above.

    Tom
     
    tom_usenet, Nov 17, 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. Trevor Lango

    Casting Away Constness

    Trevor Lango, Jan 2, 2004, in forum: C++
    Replies:
    15
    Views:
    2,436
    Ron Natalie
    Jan 2, 2004
  2. Replies:
    14
    Views:
    841
    Ian Collins
    Apr 4, 2006
  3. Laurent Deniau

    casting away constness

    Laurent Deniau, Sep 12, 2007, in forum: C Programming
    Replies:
    3
    Views:
    2,110
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F
    Sep 12, 2007
  4. copx

    casting const away

    copx, Jan 6, 2011, in forum: C Programming
    Replies:
    58
    Views:
    1,406
    Michael Press
    Feb 10, 2011
  5. Ben Bacarisse

    Re: Few questions about casting away const.

    Ben Bacarisse, Mar 4, 2014, in forum: C Programming
    Replies:
    0
    Views:
    91
    Ben Bacarisse
    Mar 4, 2014
Loading...

Share This Page