Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::string header only in Visual Studio?

Tags:

c++

visual-c++

qt

It looks like std::string is a header only file at Community/VC/Tools/MSVC/?/include/xstring, and all generated code should be included inside a build target.

If I'm correct, how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?

Update 1:

I got many downvotes for this question so let me explain why I decided to ask it.

I'm faced with strange crash, and I can not understand why this happen. I use latest Qt 5.13.0 (MSVC2017_x64) and also I have some external libraries compiled with Visual Studio 2017. All have /MDd, I checked this with dumpbin util.

When I try to run any code that invokes Qt library and std::string, I'm getting wrong result (and crash at the end).

Here is very simple example:

#include <QApplication.h>

int main(int argc, char** argv) {
    QString s1("Test");
    std::string s2 = s1.toStdString(); // here we have s2 variable with wrong internal structure
    return 0;
}

My idea was that QtCore DLL library has std::string with internal structure not compatible with std::string from Visual Studio 2017. But Qt was created with Visual Studio 2017 (maybe not same as my current Visual Studio, because there was several minor releases), so I decided to ask here if they are compatible or not.

Update 2:

Problem was in _ITERATOR_DEBUG_LEVEL. Looks like Qt was compiled with level 2 and all my external libraries and application were compiled with level 0.

This option affects internal structure of many C++ standard library classes and introduce such side effects. So when we are inside toStdString() and create std::string, we have level 2 and one internal structure. When we are in application code, we have level 0 and another internal structure. We assign object with one internal structure to object with another.

Anyway now I have better understanding of some internals.

like image 910
John Tracid Avatar asked Jun 27 '19 17:06

John Tracid


2 Answers

how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?

Because they make a decision to guarantee that, or to not guarantee that.

For example, Visual Studio 2015 to 2019 are binary compatible.

That's a decision that was made, to do that. The result, if what you say is true, is that some of the implementation specifics of std::string on that platform are frozen. This is not unusual for libraries. libstdc++'s std::list::size was non-compliant to C++11 for many years, because they could not add a needed member variable without breaking binary compatibility.

In short, this is basically a project management decision, and if they ever change the header in a way that breaks things, they will tell you that binary compatibility has been broken and you need to rebuild and relink things accordingly.

As for your Qt issue, it does smell like a binary compatibility issue. But you say that both Qt and your application have been built in Visual Studio 2017 with /MDd, which would appear to rule that out. I would ask the Qt community for further help, possibly with slightly more information about your environment and about where you obtained Qt. Also ensure that you're using the version of Qt that's intended — perhaps there are multiple installations? Which one's on your include path?

like image 70
Lightness Races in Orbit Avatar answered Oct 22 '22 18:10

Lightness Races in Orbit


Is std::string header only in Visual Studio?

Not entirely, it also depends on parts of the Standard C++ library that are implemented in Microsoft's Visual C++ Runtime. Building a binary with MSVC requires the VC++ runtimes to be linked. Only static libraries may be built without linking to a runtime, and then you must be careful to include none of the headers that require the runtime.

Is the std::string header only in Visual Studio?

(I originally read the headline this way.)

std::string is part of the C++ standard. To use std::string on any platform that supports standard C++, you should use #include <string>. It is a standard header available with pretty much any C++ compiler.

Every compiler or platform may implement the standard in its own way though. For example with MSVC you can see that xstring is how Microsoft implements std::string under the hood. If you include xstring.h directly you are writing code that depends on the version of MSVC that provides that header. That code would not be portable to other compilers.

If I'm correct, how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?

Microsoft does not guarantee that the next Visual Studio version will have the same std::string internal structure. In the past the implementation of the standard library has changed with every VC++ runtime release, which is why Windows users end up having dozens of VC++ runtime versions installed in their Add/Remove programs list.

Thankfully Microsoft has given us a guarantee that Visual Studio 2015, 2017, and 2019 all use a binary-compatible C++ runtime. This means that binaries built using the standard library provided in Visual Studio 2015 are compatible with binaries built using 2017 and 2019 too. There is no guarantee (yet) that a future version of Visual Studio will not change the VC++ runtime implemenation again.

like image 4
Romen Avatar answered Oct 22 '22 17:10

Romen