4

I've only learned a little bit of ATL in the last couple of days (after realizing how much pain pure Win32 is) and also learned about WTL and MFC, and from what I see, there are quite a few different string classes available for me.

I used to do something like this:

#include <tchar.h>
#include <string>
namespace std { typedef basic_string<TCHAR> _tstring; }

and then use _tstring everywhere in my code. After learning some ATL, I learned that there's a CString class in atltmp.h. Apparently, there's another CString class in WTL, and yet another CString class in MFC.

I have no idea whether I will stick with ATL or whether I'll switch to WTL, MFC, or something else. But right now, I'm in the process of converting my Win32 code to ATL, and I'm not sure what to change and what to keep.

Should I make my strings use CString instead of _tstring? Is there any benefit in doing so, considering both executable size (excluding shared libraries) and portability/compatibility?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • The only thing you can *legally* add to the `std` namespace is a template specialization. Also, I don't know about WTL, but the CString classes in ATL and MFC are the same, they're both typedefs of CSimpleStringT (I think) – Praetorian Aug 26 '11 at 18:09
  • 1
    @Praetorian: Eh, I don't mind engaging in such a non-intrusive illegal activity in a personal project. :P – user541686 Aug 26 '11 at 18:11
  • 4
    I looked up CString on Google the first time I learned about it. NSFW, as it turns out. – Dawson Aug 26 '11 at 19:01
  • @Toolbox: Oh LOL I'd never tried that (I'd always done `CString class`). Thanks for the warning. XD – user541686 Aug 26 '11 at 19:03
  • @Praetorian : `CString` in ATL and MFC are indeed the same, however ATL is considered the canonical owner (it's in namespace `ATL` after all...). WTL doesn't have its own that I recall -- I think it just uses ATL's. – ildjarn Aug 26 '11 at 19:17

2 Answers2

6

My preference would be to stick with CString for ATL / MFC / WTL. It's not like you have much of an option for portability if you're using those frameworks anyway; and you know what they say: When in Rome ...

Also, CString does have a few niceties about it

  • You can load resource strings from executables using CString::LoadString
  • Get direct access to the internal string buffer using CString::GetBuffer/ReleaseBuffer
  • Silently convert between CStringA & CStringW
  • Perform printf-like formatting using CString::Format
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • You can get access to the internal buffer of `basic_string` too -- just do `&str[0]`. But I especially like the 3rd and 4th points -- I think I might drink the cool-aid after all. +1 – user541686 Aug 26 '11 at 19:02
  • Oh, question: Does using `CString` have a noticeable effect on the executable size by any chance? I don't expect that it will, but I don't want to add 100 KiB to an 80-KiB executable just to change string classes. – user541686 Aug 26 '11 at 19:05
  • If you use statically linked MFC you start out with a 3000 kB, so it doesn't matter that much. :-> – Bo Persson Aug 26 '11 at 19:57
  • Also, CStrings are referenced counted, making copying cheap. (http://msdn.microsoft.com/en-us/library/aa296565(v=vs.60).aspx#_core_cstring_reference_counting) I have yet to see a std::string that is referenced counted (I don't know it the standard allows this). – Andrew Stein Aug 26 '11 at 19:59
  • @Andrew Stein C++03 does not forbid reference counted implementations of `std::string`; don't know about C++0x. Also, I remember reading somewhere that libstdc++ does implement reference counting for `std::string` – Praetorian Aug 26 '11 at 20:08
  • @Andrew: OK then I'll definitely use CString, reference counting is something I've always wanted. :) Thanks! – user541686 Aug 26 '11 at 20:19
  • @Praetorian: I turns out that my version of ATL (2.1, from the Windows 2003 DDK) doesn't have any method to *remove* characters. Guess I'll stick with `basic_string` then... – user541686 Aug 26 '11 at 21:09
  • @Mehrdad So all the member functions Remove, Mid, Left, Right are missing? – Praetorian Aug 26 '11 at 21:21
  • @Praetorian: Oh whoops, my bad. I was looking more for things like `Substring`, `Remove`, etc... those definitely exist, but I totally missed them. Thanks! – user541686 Aug 26 '11 at 23:50
  • @Prætorian *silently* convert? I always thought you had to use the macros `CT2W`, etc. – Mike Caron Feb 02 '12 at 20:15
0

Something that I just read is that CString does not support null characters.

I guess I'll keep with STL, then.

user541686
  • 205,094
  • 128
  • 528
  • 886