Why members of std::basic_string are public in VS2010?
c++, visual-studio-2010, visual-studio-2012
Solution
It's not something that affects conformance. Standard C++ programs are not allowed to use `_Mysize` in any context, not even to test if such a member exists on any standard library type. The fact that you do so anyway means the standard imposes no requirements whatsoever on the behaviour of your program.
2.11 Identifiers [lex.name]
3 In addition, some identifiers are reserved for use by C++ implementations and standard libraries (17.6.4.3.2) and shall not be used otherwise; no diagnostic is required.
17.6.4.3.2 Global names [global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
-- Each name that contains a double underscore `__` or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
In some cases, it might make it easier for MS to write highly-optimised special non-member functions in additional headers that did not exist at the time of the initial release, without modifying the original headers. That's one of the few cases where direct access to the classes' internals is defensible, and also one of the cases where the new functions cannot be declared `friend`, so if such functions are anticipated, exposing the internals may be the best (read: least bad) thing to do.
So I wouldn't be too quick yet in calling it a bug. That said, it is a questionable design decision, and I would certainly not suggest anyone copy that style.
Problem
``` #include <iostream> #include <string> int main() { std::string s; s._Mysize = 7; // Well compiled !!! std::cout << s.size() << '\n'; // prints 7 !!! } ``` Why non-static members of the `std::basic_string` are public in VS2010 ? Is this bug ? If yes, how about of next version of visual studio's ( vs2012 and vs2013) ? EDIT: I just test other containers, and ... interesting vector and unique_ptr's non-static members are public, also. ``` std::vector<char> v; v._Myfirst = (char*)2; // Well Compiled. std::unique_ptr< int > u; u._Myptr = 0; // well compiled. ``` Q: Is here any reason or advantage of using `public` data members ?