C++20引入了新属性:[[no_unique_address]]
,这表明数据成员不必具有不同于其类的所有其他非静态数据成员的地址。
所以我尝试使用这个新属性来实现我自己的向量,它将分配器作为类成员:
template <class T, class Allocator = std::allocator<T>>
class my_vector {
T* begin_;
std::size_t size_, cap_;
[[no_unique_address]] Allocator alloc_;
public:
my_vector() = default;
my_vector(const my_vector& v)
: cap_(0), alloc_(std::allocator_traits<Allocator>::select_on_container_copy_construction(v.alloc_)) { }
};
但是当我调用复制构造函数时:
my_vector<int> v;
auto v2 = v;
GCC发生编译错误:
<source>: In copy constructor 'my_vector<T, Allocator>::my_vector(const my_vector<T, Allocator>&) [with T = int; Allocator = std::allocator<int>]':
<source>:13:104: internal compiler error: in assign_temp, at function.c:984
13 | : cap_(0), alloc_(std::allocator_traits<Allocator>::select_on_container_copy_construction(v.alloc_)) { }
但是clang和MSVC都可以正确编译。 神箭
那么这是GCC的bug吗?
那么这是GCC的bug吗?
可能吧。
内部编译器错误
并不意味着编译的代码中存在bug。 不管代码是否正确,ICE可能是编译器的bug,也可能是坏掉的安装。