开发者

How do I swap an MFC CString?

OK, so I'm all sold on the copy-and-swap idiom and I think I mostly know how to implement it.

However, or codebase uses MFC's CString class as string and this ain't gonna change.

Since swap must (should???) be nothrow, I cannot do

std::swap(this->my_cstring, rhs.my_cstring);

since that will create a temporary CString object which may throw. (Plus its inefficient.)

So where I'm left? Should I add a try-catch? Should I actually allow this (well, extremely rare) out of memory condition to raise an exception and make swap fail?

Looking at CStrings implementation, it doesn'开发者_C百科t seem there's a member or function that allows for swapping ...


Self-Answer:

After looking into CString more closely, it appears that due to the fact the CString is a reference counted string implementation, swapping it via std::swap is actually "99%" exception safe because all that happens is some reference count increments and decrements.

It's only "99%" safe, as when the CString object IsLocked, it will always do a copy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜