stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: test for lib.string.access
Date Thu, 04 May 2006 20:38:17 GMT
Anton Pevtsov wrote:
> I am porting the test for lib.string.access methods (operator[] and at)
> and found one issue.
> 
> The code 
> 
> #include <string>
> 
> int main (void)
> {
>     std::string s ("a", 1);
>     const char& c = s[1];
> 
>     return 0;
> }
> 
> results in assertion and fails. But the standard says for 
> const_reference operator[] (size_type pos) const:

But that's not the function that gets called in the test. The
string object is not const so the non-const overload of the
operator is invoked.

To invoke the const overload on a non-const object a cast is
necessary:

     const char& c = const_cast<const std::string&>(s)[1];

Martin

Mime
View raw message