In C++11, can the characters in the array pointed to by string::c_str() be altered?

后端 未结 4 739
情深已故
情深已故 2021-01-01 21:57

std::string::c_str() returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of t

4条回答
  •  既然无缘
    2021-01-01 22:26

    I'd say that if c_str() returns a const char * then its not ok, even if it can be argued to be a gray area by a language lawyer.

    The way I see it is simple. The signature of the method states that the pointer it returns should not be used to modify anything.

    In addition, as other commenters have pointed out, there are other ways to do the same thing that do not violate any contracts. So it's definitely not ok to do so.

    That said, Borgleader has found that the language still says it isn't.

提交回复
热议问题