有没有办法使 std::string 引用外部提供的缓冲区但不拥有它?
Is there a way make a std::string that references an externally provided buffer but not own it?
基本上,如果一个人有一个空终止字符串的预加载缓冲区和要引用的长度,并且想将对它的引用传递给采用 std::string & 但不复制字符串的方法或有没有拥有,是否可以拥有?
这只会有一个有限的生命周期,其管理方式是仅在缓冲区有效时才有效。
Is there a way make a std::string that references an externally provided buffer but not own it?
没有
您有以下选择:
- 首先使用
std::string
作为“外部”缓冲区。
- 将外部缓冲区复制到字符串中。
- 不要使用(引用)
std::string
作为参数。
std::string_view
通常是一个不错的选择。但是,创建 non-null 终止字符串视图非常容易,并且您的前提明确声明空终止。如果这很重要,那么您可能需要避免使用字符串视图。
- 如果字符串视图不合适,那么您可以使用
const char*
指向空终止字符串。
基本上,非拥有字符串的答案是否定的。
但是,如果非拥有标准不是那么重要,您可以使用自己的分配器来引用特定缓冲区。
您还可以使用 std::pmr::string
来自定义 memory_resource
.
思路如下:
#include <string>
#include <memory_resource>
#include <array>
#include <utility>
#include <iostream>
template<std::size_t size>
class StackBufferResource {
public:
auto as_resource() {return &m_resource;}
auto address() {return m_buffer.data();}
private:
std::array<std::byte, size> m_buffer{};
std::pmr::monotonic_buffer_resource m_resource{m_buffer.data(), size};
};
int main() {
StackBufferResource<512> buffer;
std::pmr::string myString("My name is Antoine and I am not sure for this answer", buffer.as_resource());
std::cout << myString << "\n";
std::cout << (const char*)buffer.address() << std::endl;
}
std::pmr::monotonic_buffer_resource
是一个不断增长的 memory_resource
对象。这意味着释放是一种“无操作”。
这种东西的好处是你可以把同样的东西给std::pmr::vector
。
不过,您可以注意以下几点:
std::pmr::string
使用 char
。由于它是一个微不足道的对象,我认为(虽然我不确定)在字符串被销毁后访问缓冲区内存是安全的。
如果它是一种不易破坏的类型,我认为有可能看到垃圾值。
基本上,如果一个人有一个空终止字符串的预加载缓冲区和要引用的长度,并且想将对它的引用传递给采用 std::string & 但不复制字符串的方法或有没有拥有,是否可以拥有?
这只会有一个有限的生命周期,其管理方式是仅在缓冲区有效时才有效。
Is there a way make a std::string that references an externally provided buffer but not own it?
没有
您有以下选择:
- 首先使用
std::string
作为“外部”缓冲区。 - 将外部缓冲区复制到字符串中。
- 不要使用(引用)
std::string
作为参数。std::string_view
通常是一个不错的选择。但是,创建 non-null 终止字符串视图非常容易,并且您的前提明确声明空终止。如果这很重要,那么您可能需要避免使用字符串视图。- 如果字符串视图不合适,那么您可以使用
const char*
指向空终止字符串。
基本上,非拥有字符串的答案是否定的。
但是,如果非拥有标准不是那么重要,您可以使用自己的分配器来引用特定缓冲区。
您还可以使用 std::pmr::string
来自定义 memory_resource
.
思路如下:
#include <string>
#include <memory_resource>
#include <array>
#include <utility>
#include <iostream>
template<std::size_t size>
class StackBufferResource {
public:
auto as_resource() {return &m_resource;}
auto address() {return m_buffer.data();}
private:
std::array<std::byte, size> m_buffer{};
std::pmr::monotonic_buffer_resource m_resource{m_buffer.data(), size};
};
int main() {
StackBufferResource<512> buffer;
std::pmr::string myString("My name is Antoine and I am not sure for this answer", buffer.as_resource());
std::cout << myString << "\n";
std::cout << (const char*)buffer.address() << std::endl;
}
std::pmr::monotonic_buffer_resource
是一个不断增长的 memory_resource
对象。这意味着释放是一种“无操作”。
这种东西的好处是你可以把同样的东西给std::pmr::vector
。
不过,您可以注意以下几点:
std::pmr::string
使用 char
。由于它是一个微不足道的对象,我认为(虽然我不确定)在字符串被销毁后访问缓冲区内存是安全的。
如果它是一种不易破坏的类型,我认为有可能看到垃圾值。