在用 STL 和 Boost 的,都是什么人?

发布时间:
2024-05-08 11:35
阅读量:
2

告诉同学们一个惊人的事实:

狭义上的 C 风格的 Win32 API(不含 COM、C++ 的)自 2017 年起就几乎没有变动!(在我所知的范围内只有 esent.h 和安全相关的几个头文件有那么三两个新增 API,GDI+ 增加几个非正式公开 API 等。)

Windows 的绝大多数(话说严谨一点)新特性和新功能都是通过 COM 接口、C++ 甚至是 .NET API 提供的。(当然,.NET 原名也叫 COM+,而 C++/WinRT 被戏称为 COM++。)

换句话说,Windows 用户态 API 早在 2017 年就全面转成 C++ 了!

而现在 乎上有些人却在倡导就算用 C++,也只用 C 的那部分,例如不用 STL。(std::string 可能是个例外,笑)

这是什么奇观!一听到 modern C++,就发作 cyberpsychosis。(+ 读作 plus 以便押韵)

放一段 C++ 代码看看 Windows 编程的现状:(跟写 C# 似的。)

#include <string> #include <iostream> #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.Web.Http.h> #include <winrt/Windows.Storage.h> #include <winrt/Windows.Storage.Streams.h> using namespace winrt; using namespace Windows::Foundation; using namespace Windows::Web::Http; using namespace Windows::Storage; using namespace Windows::Storage::Streams; IAsyncAction DownloadToFile(std::wstring &url, std::wstring &dirPath) { HttpClient client; auto response = co_await client.GetAsync(Uri(url)); response.EnsureSuccessStatusCode(); auto buffer = co_await response.Content().ReadAsBufferAsync(); auto localFolder = co_await StorageFolder::GetFolderFromPathAsync(dirPath); auto file = co_await localFolder.CreateFileAsync(L"downloaded.html", CreationCollisionOption::ReplaceExisting); co_await FileIO::WriteBufferAsync(file, buffer); co_return; } IAsyncOperation<int> Main() { std::wstring url = __argc > 1 ? __wargv[1] : L"https://aka.ms/cppwinrt"; std::wstring dir_path = __argc > 2 ? __wargv[2] : L"C:\\Temp"; try { co_await DownloadToFile(url, dir_path); std::wcout << "Done." << std::endl; co_return 0; } catch (hresult_error const &ex) { std::wcout << "Error: " << std::wstring_view(ex.message()) << std::endl; co_return 1; } } int main() { init_apartment(); return Main().get(); }


不用怀疑,boost 就是民间 std::experimental 和 std::trN。时不时会转正的。

END