Hello!

I thought, value_types for standard containers had to be assignable,
thus excluding const objects. However, VC++ 7.1 compiles the following
code without any complaints, even with the /Za - Option.

What am I missing?


#include <list>

int main () {

std::list<const int> l2;
l2.push_back(1);
*l2.begin() = 3;
return 0;
}

Re: stl containers for const objects by Hendrik

Hendrik
Fri Feb 06 03:40:48 CST 2004

Christian Jaeger <cj@sim.bepr.ethz.ch> wrote:
> [...]
> What am I missing?

Another compiler? Comeau Online says this:

"stl_alloc.h", line 610: error: function
"std::allocator<_Tp>::address(std::allocator<_Tp>::reference) const
[with _Tp=const int]" has already been declared
const_pointer address(const_reference __x) const { return &__x; }
^
detected during:
instantiation of class "std::allocator<_Tp> [with _Tp=const int]"
at line 336 of "stl_list.h"
instantiation of "std::list<_Tp, _Alloc>::list(const
std::list<_Tp, _Alloc>::allocator_type &) [with
_Tp=const int, _Alloc=std::allocator<const int>]" at
line 5 of "ComeauTest.c"

"stl_list.h", line 49: warning: class "std::_List_node<const int>" defines
no constructor to initialize the following:
const member "std::_List_node<_Tp>::_M_data [with _Tp=const int]"
struct _List_node : public _List_node_base {
^
detected during:
instantiation of class "std::_List_node<_Tp> [with _Tp=const int]"
at line 195 of "stl_alloc.h"
instantiation of "_Tp *std::simple_alloc<_Tp,
_Alloc>::allocate(size_t) [with
_Tp=std::_List_node<const int>, _Alloc=std::alloc]" at
line 184
instantiation of "std::_List_node<_Tp> *std::_List_alloc_base<_Tp,
_Allocator, true>::_M_get_node() [with _Tp=const int,
_Allocator=std::allocator<const int>]" at line 209
instantiation of "std::_List_base<_Tp, _Alloc>::_List_base(const
std::_List_base<_Tp, _Alloc>::allocator_type &) [with
_Tp=const int, _Alloc=std::allocator<const int>]" at
line 336
instantiation of "std::list<_Tp, _Alloc>::list(const
std::list<_Tp, _Alloc>::allocator_type &) [with
_Tp=const int, _Alloc=std::allocator<const int>]" at
line 5 of "ComeauTest.c"

"ComeauTest.c", line 7: error: expression must be a modifiable lvalue
*l2.begin() = 3;
^

2 errors detected in the compilation of "ComeauTest.c".

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers



Re: stl containers for const objects by P

P
Fri Feb 06 08:03:28 CST 2004

"Christian Jaeger" <cj@sim.bepr.ethz.ch> wrote in message
news:40235c0d$1@pfaff2.ethz.ch...

> I thought, value_types for standard containers had to be assignable,
> thus excluding const objects. However, VC++ 7.1 compiles the following
> code without any complaints, even with the /Za - Option.
>
> What am I missing?
>
>
> #include <list>
>
> int main () {
>
> std::list<const int> l2;
> l2.push_back(1);
> *l2.begin() = 3;
> return 0;
> }

By popular demand, we made node-based containers const tolerant
as a conforming extension.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com