简体   繁体   English

std::vector 的概念和 GCC 实现

[英]Concept and GCC implementation of std::vector

Let's try to create a pointer-like type matching both the RandomAccessIterator and the NullablePointer concepts.让我们尝试创建一个与RandomAccessIteratorNullablePointer概念相匹配的类似指针的类型。 The goal here is to create a custom Allocator in order to use the std::vector with a our pointer-like type.这里的目标是创建一个自定义分配器,以便将 std::vector 与我们的指针类型一起使用。 You can found the snippet here你可以在这里找到片段

The problem arises when trying to compile this code :尝试编译此代码时出现问题:

int main()
{
     std::vector<float, allocator<float>> t {0.f, 0.f};
}

We get the following error message :我们收到以下错误消息:

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:173:6: error: 
  value of type 'pointer' (aka 'ptr_like<int>') is not contextually convertible
  to 'bool'
    if (__p)

Here we see that our type must be bool convertible.在这里我们看到我们的类型必须是 bool 可转换的。 That's not hard to do, and if people have an instance of our pointer type, they're likely going to use it like this anyway.这并不难做到,如果人们有我们的指针类型的实例,他们很可能会像这样使用它。 Thus let's do it and uncomment the following to our snippet :因此,让我们这样做并取消对我们的代码段的以下注释:

// In detail::ptr_like we add :
operator bool() const;

And we get the following error with clang :我们用 clang 得到以下错误:

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:168:25: error: 
  conditional expression is ambiguous; 'pointer' (aka 'ptr_like<int>') can be
  converted to 'int' and vice versa
  { return __n != 0 ? _M_impl.allocate(__n) : 0; }

Clang's error shows us exactly why we get into trouble here. Clang 的错误向我们展示了我们在这里遇到麻烦的确切原因。 Now the only viable solutions that I've found are the following :现在,我发现的唯一可行的解​​决方案如下:

  • Replacing 0 with the c++11 keyword nullptr when c++11 is requested请求 c++11 时用 c++11 关键字nullptr替换0
  • Replacing 0 by a cast to the pointer type static_cast<pointer>(0)0替换为指针类型static_cast<pointer>(0)
  • Updating the concept requirement for pointer in the Allocator concept.更新分配器概念中指针的概念要求。
  • No using the constructor with std::initializer_list (sad)不使用带有std::initializer_list的构造std::initializer_list (悲伤)

Is it wrong to defines custom pointers in C++ ?在 C++ 中定义自定义指针是错误的吗?

Is it a bug ?这是一个错误吗?

Expanding on my comment.扩展我的评论。

{ return __n != 0 ? _M_impl.allocate(__n) : 0; }

The first result can convert to bool to int .第一个结果可以转换为boolint The second result can convert to int .第二个结果可以转换为int This is just as good a result as converting the second result to pointer , so it is ambiguous.这与将第二个结果转换为pointer一样好,所以它是不明确的。

But we don't want the bool conversion to be available here, so we can make it explicit .但是我们不希望bool转换在这里可用,所以我们可以让它explicit It will still be available in logical conditions and the like, as I describe here .正如我在此处描述的那样,它仍然可以在逻辑条件等条件下使用。 Being contextually convertable to bool is another condition The Standard places on custom pointer types satisfying the NullablePointer requirements (since 2011 - see 17.6.3.3/3).在上下文中可转换为bool是标准放置在满足NullablePointer要求的自定义指针类型上的另一个条件(自 2011 年 - 见 17.6.3.3/3)。

I suggest to define the conversion operator as explicit.我建议将转换运算符定义为显式。 For example例如

explicit operator bool() const;

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

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