Does Visual Studio 2012 (VC11) implement the C++11 guarantee that std::string uses contiguous memory?

StackOverflow https://stackoverflow.com/questions/22751650

I've seen a few questions asking this from before C++11 started being implemented by compilers. VS2012 currently implements some C++11 functionality, but has plenty of unimplemented C++11 features. I cannot find mention of whether strings are now guaranteed contiguous or not, and am wondering if they are.

The C++11 guarantee of contiguousness is item 21.4.1.5 of this working draft of the standard.

If possible I would like an official citation with the answer.

有帮助吗?

解决方案

Visual C++ has always used contiguous storage for std::string.

One of the design decisions for implementing the contiguity requirement was that all current compilers were doing that anyway. Microsoft is quite well represented on the ISO C++ committee; there's no chance that they overlooked it. Visual C++ did go through a phase of implementing a copy-on-write "optimization" but never was one of the ones using non-contiguous "ropes".

其他提示

I don't know in detail what the standard says, but std::string has the very interesting method c_str() that returns a const char* to the null-terminated string.

I would be crazy to have strings implemented not using contiguous memory. Especially because c_str() is also const.

So, applying the Occam's razor, we can say that it should be safe. And it has nothing to do with C++11, it's applicable also to the older standards.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top