Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it reasonable to use std::basic_string<t> as a contiguous buffer when targeting C++03?
    primarykey
    data
    text
    <p>I know that in C++03, technically the <code>std::basic_string</code> template is not required to have contiguous memory. However, I'm curious how many implementations exist for modern compilers that actually take advantage of this freedom. For example, if one wants to use <code>basic_string</code> to receive the results of some C API (like the example below), it seems silly to allocate a vector just to turn it into a string immediately.</p> <p>Example:</p> <pre><code>DWORD valueLength = 0; DWORD type; LONG errorCheck = RegQueryValueExW( hWin32, value.c_str(), NULL, &amp;type, NULL, &amp;valueLength); if (errorCheck != ERROR_SUCCESS) WindowsApiException::Throw(errorCheck); else if (valueLength == 0) return std::wstring(); std::wstring buffer; do { buffer.resize(valueLength/sizeof(wchar_t)); errorCheck = RegQueryValueExW( hWin32, value.c_str(), NULL, &amp;type, &amp;buffer[0], &amp;valueLength); } while (errorCheck == ERROR_MORE_DATA); if (errorCheck != ERROR_SUCCESS) WindowsApiException::Throw(errorCheck); return buffer; </code></pre> <p>I know code like this might slightly reduce portability because it implies that <code>std::wstring</code> is contiguous -- but I'm wondering just how unportable that makes this code. Put another way, how may compilers actually take advantage of the freedom having noncontiguous memory allows?</p> <hr> <p>EDIT: I updated this question to mention C++03. Readers should note that when targeting C++11, the standard now requires that <code>basic_string</code> be contiguous, so the above question is a non issue when targeting that standard.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload