这是我在进行 Visual Studio 2019 代码分析后得到的代码(警告 C26409 避免显式调用 new 和 delete,请使用 std::make_unique (r.11)。):
#include <windows.h>
#include <strsafe.h>
int main()
{
auto *sResult = new WCHAR[256];
StringCchPrintfW(sResult, 256, L"this is a %s", L"test");
delete[] sResult;
}
Run Code Online (Sandbox Code Playgroud)
我原本假设使用 new/delete 而不是 calloc/free,但现在编译器告诉我使用 std::make_unique。我找不到任何有关如何更改代码以使其兼容的示例。
所以我的问题是:
如何更改我的代码,使其不使用 new/delete
为什么我不应该使用 new/delte 与 std::make_unique ?