是否合理使用std :: basic_string< t>作为连接缓冲区时目标C ++ 03? [英] Is it reasonable to use std::basic_string<t> as a contiguous buffer when targeting C++03?

查看:148
本文介绍了是否合理使用std :: basic_string< t>作为连接缓冲区时目标C ++ 03?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在C ++ 03中,技术上 std :: basic_string 模板不需要有连续的内存。然而,我很好奇现代编译器实际利用这种自由有多少实现存在。例如,如果想要使用 basic_string 接收一些C API的结果(如下面的示例),似乎只是为了将它分配给一个向量

I know that in C++03, technically the std::basic_string 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 basic_string 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.

例如:

DWORD valueLength = 0;
DWORD type;
LONG errorCheck = RegQueryValueExW(
        hWin32,
        value.c_str(),
        NULL,
        &type,
        NULL,
        &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,
            &type,
            &buffer[0],
            &valueLength);
} while (errorCheck == ERROR_MORE_DATA);

if (errorCheck != ERROR_SUCCESS)
    WindowsApiException::Throw(errorCheck);

return buffer;

我知道这样的代码可能会稍微减少可移植性,因为它意味着 std: :wstring 是连续的 - 但我想知道如何不便宜,使这个代码。换句话说,编译器如何实际利用了具有不连续内存的自由?

I know code like this might slightly reduce portability because it implies that std::wstring 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?

编辑:我更新了这个问题提到C ++ 03。读者应该注意,当定位C ++ 11时,标准现在要求 basic_string 是连续的,因此上面的问题是针对该标准的一个非问题。

I updated this question to mention C++03. Readers should note that when targeting C++11, the standard now requires that basic_string be contiguous, so the above question is a non issue when targeting that standard.

推荐答案

我认为假设std :: string连续分配存储是相当安全的。

I'd consider it quite safe to assume that std::string allocates its storage contiguously.

目前, std :: string 的所有已知实现都连续分配空间。

At the present time, all known implementations of std::string allocate space contiguously.

此外,目前的草稿C ++ 0x( N3000 )要求连续分配空间(§21.4.1/ 5):

Moreover, the current draft of C++ 0x (N3000) requires that the space be allocated contiguously (§21.4.1/5):



basic_string对象中的类char对象应该连续存储
。也就是说,对于任何
basic_string对象,标识
& *(s.begin()+ n)==& * s.begin()+ n
n的所有值如
,0 <= n <

The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

因此,当前或未来实现 std ::的机会string 使用非连续存储基本上为零。

As such, the chances of a current or future implementation of std::string using non-contiguous storage are essentially nil.

这篇关于是否合理使用std :: basic_string&lt; t&gt;作为连接缓冲区时目标C ++ 03?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆