标签: C++ 文|吴羽舒

CStdString

Joe O'Leary CStdString 其本身好像是VC++/MFC中的一部分,支持ansi、unicode:

StdString.h

2011-12-05 的版本(没找到更新版本):

//  FILE:  StdString.h
//  AUTHOR:	Joe O'Leary (with outside help noted in comments)
//
//		If you find any bugs in this code, please let me know:
//
//				jmoleary@earthlink.net
//				http://www.joeo.net/stdstring.htm (a bit outdated)
//
//      The latest version of this code should always be available at the
//      following link:
//
//              http://www.joeo.net/code/StdString.zip (Dec 6, 2003)
//
//
//  REMARKS:
//		This header file declares the CStdStr template.  This template derives
//		the Standard C++ Library basic_string<> template and add to it the
//		the following conveniences:
//			- The full MFC CString set of functions (including implicit cast)
//			- writing to/reading from COM IStream interfaces
//			- Functional objects for use in STL algorithms
//
//		From this template, we intstantiate two classes:  CStdStringA and
//		CStdStringW.  The name "CStdString" is just a #define of one of these,
//		based upone the UNICODE macro setting
//
//		This header also declares our own version of the MFC/ATL UNICODE-MBCS
//		conversion macros.  Our version looks exactly like the Microsoft's to
//		facilitate portability.

别人的言论、评价

一个VC++中的StdString类源代码,可完善替代CString字符串类,支持ansi、unicode。实际在一些项目中用了一段,感觉非常棒,推荐给朋友们。
在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌。

CStdString类实现了CString的功能,支持跨平台。

mixing-objc-and-c-with-c-template-classes-in-more-than-one-source 上获取到这些信息:

Joe's CStdString is in fact a derived class from std::string.

Anyway, bad thing - good thing: CStdString has no own members. It's a straightforward wrapper for one/two byte instantiations of std::basic_string with the convenience of most features that MFC CString offers and a wide usability. I used it on Linux more than 10 years ago and on Win32 where I couldn't use MFC. Joe's work on it started 1998 with help of ~50 people within 7 years of development. I think they've managed the poorly side effects being in the template.

CString-clone Using Standard C++

As much as I use and appreciate the Standard C++ Library, I've never liked its string template - basic_string<>. At times, it seems the designers went out of their way to make it difficult to use.

On the other hand, I've always loved the ease of use of MFC's CString class. It checks for NULL pointers, implicitly converts to const TCHAR*, and has some very handy member functions (Format, Load, etc.) that make string programming a breeze. But of course, I don't want to use MFC anymore. In fact, I don't want to rely on any proprietary library because I want portability.
Go to Parent(I realize this response is over a year late. I don't appear to be being notified when people reply to this article)

"Performance" is a fairly encompassing term. Most of the performance differences will come down to the performance of std::string vs CString. That depends upon your particular implementation of basic_string, over which I have no control

For the performance of various CString-facade functions (e.g. format), well at this point I am long past comparing those or worrying about it. It is not going to make a difference in 99% of the cases. You would have to be doing some monster-level of string processing for it to matter.

If that sounds like a bit of a dismissal, I suppose it is. This class is mostly meant to help people transition over from CString to std::string with a minimum of code-rewrite. To help you get off of MFC> The idea is that you can get your code running right away and then start refactoring any important string processing code to use basic_string properly, in the most efficient way you see fit. This class is not meant to be used alongside CString. But if it must, well you can then always use CString instead. This class coexist well with it.

-Joe