gpt4 book ai didi

c++ - wchar_t 对 Windows API 有用吗?

转载 作者:可可西里 更新时间:2023-11-01 09:44:37 24 4
gpt4 key购买 nike

当我 #include <windows.h>在 C 或 C++ 中,我被迫决定字符的格式,其中 TCHAR要么等于 charwchar_t .

我四处看了很多,就像 this one 这样的帖子或类似 this 的网站指出 wchar_t事情发生在 UTF8 之前的很久以前,并且由于各种原因,在现代编程中并不是一个特别好的 Unicode 解决方案。然而,这些并没有说明对已经在 wchar_t 中运行的现有系统的支持。 .

所以我的问题是,我应该使用哪一个?如果我使用普通的 char这会在未来被 MS 放弃吗,因为在一天结束时,wchar_t API 版本更新?或者如果我使用 wchar_t , 让我的代码在其他现代平台上运行会很痛苦吗,这些平台后来使用普通的旧 char 开发在 UTF8 中?

最佳答案

它绝对有用,也是正确处理任意路径名的唯一方法(因为它们允许包含宽字符)。 UTF-16 的选择经常受到批评(有充分的理由),但这无关紧要。操作系统使用它,所以你也必须使用它。您可以做的最好的事情是始终调用 WINAPI 函数的宽字符版本(例如 OpenFileW)并在您的程序内部使用 UTF-8。是的,这意味着来回转换,但这通常不是性能瓶颈。

我强烈推荐 UTF-8 Manifesto这解释了为什么客观上这是最好的方法。

Portability, cross-platform interoperability and simplicity are more important than interoperability with existing platform APIs. So, the best approach is to use UTF-8 narrow strings everywhere and convert them back and forth when using platform APIs that don’t support UTF-8 and accept wide strings (e.g. Windows API). Performance is seldom an issue of any relevance when dealing with string-accepting system APIs (e.g. UI code and file system APIs), and there is a great advantage to using the same encoding everywhere else in the application, so we see no sufficient reason to do otherwise.

关于c++ - wchar_t 对 Windows API 有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com