Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 20 October 2013 - 04:44 PM

That code looks pretty dodgy to me.


I should actually use &str[0] to get a non-const pointer to the internal data... oops. That'll let me avoid the const_cast, but it'll do the same thing anyway. Really, they ought to just make .data() return a non-const pointer. rolleyes.gif

 
Hmmm, come to think of it, &str[0] also ensures that compiler implementations doing clever things behind the scenes like copy-on-write strings handle the situation correctly. GCC iirc, still uses COW for relatively recent versions of the compiler. C++11 requires std::string to not be COW, but last I heard GCC hadn't yet changed their std::string implementation. So my const_cast actually could be buggy.

I still think .data() ought to return a non-const pointer. wink.png


#1Servant of the Lord

Posted 20 October 2013 - 04:43 PM

I should actually use &str[0] to get a non-const pointer to the internal data... oops. That'll let me avoid the const_cast, but it'll do the same thing anyway. Really, they ought to just make .data() return a non-const pointer. rolleyes.gif

 

Hmmm, come to think of it, &str[0] also ensures that compiler implementations doing clever things behind the scenes like copy-on-write strings handle the situation correctly. GCC iirc, still uses COW for relatively recent versions of the compiler. C++11 requires std::string to not be COW, but last I heard GCC hadn't yet changed their std::string implementation. So my const_cast actually could be buggy.


PARTNERS