. , , , , . :
#include <string>
#include <vector>
#include <windows.h>
using std::wstring;
using std::vector;
wstring getComputerName()
{
DWORD size = 1;
vector<wchar_t> buffer(size);
while ((GetComputerNameW(&buffer[0], &size) == 0))
{
if (GetLastError() != ERROR_BUFFER_OVERFLOW) aargh();
buffer.resize(++size);
};
return wstring(&buffer[0], size);
}
, , , . , , , std::wstring, , , , MSVC, , .
, wstring::reference - wchar_t&, . 21.3.4 , non-const operator[] a reference data()[pos]. , reference wchar_t&, , &buffer[0]. . , , .
It takes a lot of effort and comments to avoid copying a string, so I never felt the need to avoid an intermediate array / vector.
source
share