This seems simple to me but I’m having trouble actually finding anything explicitly stating this.
Does std::string stringArray[3] not create an array of std::string objects, the way that SomeType typeArray[3] would? The actual number is irrelevant; I just picked 3 arbitrarily.
In the Visual Studio 2010 debugger, it appears to create a single string as opposed to an array of strings. Why?
Wild guess: is it calling the default std::string constructor, and then invoking an unused access of index 3? If so, why doesn’t that cause an out of bounds exception on an empty string?
Does this have something to do with overloading the [] operator?
There are plenty of ways to code the same things without specifically using an array of std::string without issue, but what is the explanation/justification for this behavior? It seems counterintuitive to me.
Edit: I found this thread std::string Array Element Access in which the comments on the answer appear to observe the same behavior.
The OP is NOT losing his mind. But Visual Studio’s integrated debugger certainly is. I believe this is a bust in the detection of operator [] for
std::string, which is sad However, it does work correctly forstd::vector<std::string>An excellent reference to how you can get around this using the watch-window (no way to do it using the Auto or Locals window afaik) can be found at this previously posted question. This is also an extremely helpful method for viewing pointer-based dynamic arrays (arrays allocated with
Type *p = new Type[n];). Hint: use “varname,n” wherevarnameis the variable (pointer or fixed array), and ‘n‘ is the number of elements to expand.A demonstration is in order to show a declared C-array of
std::stringand what the OP was observing, and astd::vector<std::string>to show what things should look like: