Click to See Complete Forum and Search --> : std::string - how is this working?


John E
February 7th, 2008, 02:13 AM
Firstly, is std::string the same thing as basic_string? The reason I'm asking is that I can never find anything on MSDN about std::string - but basic_string seems very similar.

Anyway, here's my question - Yesterday I was looking at some code that I wrote a long time ago. It does some string comparisons and I realised that I should probably have written something like this:-

#include <string>

using namespace std;

void MyFunc ( string someString, char* compareString )
{
if ( 0 == someString.compare( string( compareString )))
; do something here....
}whereas in fact, I did it this way:-

#include <string>

using namespace std;

void MyFunc ( string someString, char* compareString )
{
if ( someString == compareString )
; do something here....
}That code appears to work just fine - even though this MSDN article (http://msdn2.microsoft.com/en-us/library/5zz6weyz(VS.80).aspx) seems to suggest that basic_string has no equality operators.

Am I looking at the wrong MSDN article? Or is it out of date? Or have I somehow been kidding myself all these years and the code doesn't really work?? :confused:

Paul McKenzie
February 7th, 2008, 02:30 AM
Firstly, is std::string the same thing as basic_string?It is is the following typedef:

namespace std { typedef basic_string<char> string; }

The reason I'm asking is that I can never find anything on MSDN about std::string - but basic_string seems very similar.You need to get a more thorough reference to the C++ standard classes than MSDN. Something like "The C++ Standard Library" by Jossutis.

That article doesn't show you not only operator ==, but operator <, operator >, etc. which are all valid.

Regards,

Paul McKenzie

treuss
February 7th, 2008, 02:31 AM
std::basic_string is a template.
std::string is an instantiation of this template, in simplified form (ignoring traits arguments) it boils down to a definition liketypedef std::basic_string<char> std::string;
std::basic_string::operator== (and thus std::string::operator==) is defined and works as you expect. If MSDN is telling something different, it probably does that to lie about superiority of CString or something along those lines.

Edit: Looks like Paul beat me to an answer.

John E
February 7th, 2008, 03:06 AM
Thanks for the info guys! :thumb:

jwbarton
February 7th, 2008, 09:27 AM
The problem with MSDN is that it isn't organized in a way that you can easily find things or learn about the C++ language. The particular article that was referenced in the first post: MSDN article (http://msdn2.microsoft.com/en-us/library/5zz6weyz(VS.80).aspx) gives information about the members of basic_string. The operator== (and other comparison operators) are not members of basic_string, but are free templated functions that take basic_string arguments.

The link: <string> Members (http://msdn2.microsoft.com/en-us/library/xabz5s9c(VS.80).aspx) gives information about the comparison operators.

If you know where to look, the MSDN library generally has the information. It just doesn't provide any easy way to find it...

Best regards,
John