简体   繁体   English

C++20 P0784R7 与 P1004R2 constexpr std::vector 相比,非瞬态分配太脆弱

[英]C++20 P0784R7 Non-transient allocation too brittle vs. P1004R2 constexpr std::vector

I would like to create an extensive constexpr data repository in C++20.我想在 C++20 中创建一个广泛的constexpr数据存储库。 Tens of thousands of hopefully native C++ objects loaded as "text segment" ie demand paged into the process and shared between instances (the code is generated).数以万计的本机 C++ 对象作为“文本段”加载,即需求分页到进程中并在实例之间共享(生成代码)。

The objects are referencing each other and need indexing by various properties, cross-referencing etc. all hopefully done at compile time.这些对象相互引用,需要通过各种属性、交叉引用等进行索引,所有这些都希望在编译时完成。 This calls for constexpr (associative) containers, but it can't be done with templatized (by size, hash size, etc.) containers, as everything needs to remain polymorphic.这需要constexpr (关联)容器,但不能使用模板化(按大小、hash 大小等)容器来完成,因为一切都需要保持多态性。

First it seems impossible, as C++20 P0784R7 says that non-transient allocation is too brittle, ie dynamically allocated memory is not allowed to be leaked out of constexpr evaluations.首先这似乎是不可能的,因为 C++20 P0784R7说非瞬态分配太脆弱,即动态分配的 memory 不允许从constexpr评估中泄露出来。

At the same time C++20 P1004R2 says that constexpr std::vector is to be supported.同时 C++20 P1004R2说要支持constexpr std::vector In the latter paper I see all members, including the modification members marked as constexpr .在后一篇论文中,我看到了所有成员,包括标记为constexpr的修改成员。 So unless I'm missing something, I can add elements to std::vectors inside constexpr evaluations (compilers don't support it yet, so I can't try).因此,除非我遗漏了什么,否则我可以在constexpr评估中向std::vectors添加元素(编译器还不支持它,所以我不能尝试)。

// 21.3.11.5, modifiers
template<class... Args> constexpr reference emplace_back(Args&&... args);
constexpr void push_back(const T& x);
constexpr void push_back(T&& x);
...

But then why can't I build an allocator<T> around a std::vector<T> and still have non-transient allocations?但是,为什么我不能围绕std::vector<T>构建一个allocator<T> T> 并且仍然具有非瞬态分配? Aren't the two capabilities "computationally equivalent" ie constexpr std::vector needs non-transient allocation to be implemented, and therefore it can also provide it ?这两种功能不是“计算等效”,即constexpr std::vector需要实现非瞬态分配,因此它也可以提供吗?

And when will compilers finally support it?编译器何时会最终支持它? ... or is this suspected contradiction even the reason for the delay? ……或者这种疑似矛盾甚至是延迟的原因?

You have a misunderstanding of transient allocations.您对瞬态分配有误解。 Transient allocations are allocations that exist entirely within constant evaluation.瞬态分配是完全存在于持续评估中的分配。 For example:例如:

constexpr std::size_t summation(std::vector<unsigned int> const &vi)
{
  std::size_t ret = 0;
  for(auto i : vi)
    ret += i;
  return ret;
}

constexpr auto sum = summation({20, 44, 98});

In the call of summation , the ultimate input source (the braced-init-list and its values) is known entirely at compile time.summation的调用中,最终的输入源(braced-init-list 及其值)在编译时是完全已知的。 Yes, there is a vector , but this exists entirely within the constant expression evaluation.是的,有一个vector ,但这完全存在于常量表达式评估中。 The vector object is used to compute the constant value, but the vector itself never leaves constant expression code. vector object 用于计算常量值,但vector本身从不留下常量表达式代码。

This kind of allocation, one that exists entirely within constant evaluation, is called a "transient allocation".这种完全存在于不断评估中的分配称为“瞬态分配”。 Such allocations are permitted under C++20's rules.根据 C++20 的规则,此类分配是允许的。

A non-transient allocation would be data "loaded as 'text segment'", and that is what isn't allowed.非瞬态分配将数据“加载为'文本段'”,这是不允许的。 So your first impression is correct.所以你的第一印象是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM