是移动构造函数
noexcept
定义了容器实现的属性?我刚刚发现以下内容可以在clang中使用,但不能在gcc或msvc++中使用:
std::vector<std::vector<std::unique_ptr<int>>> vector_a;
std::vector<std::stack<std::unique_ptr<int>>> vector_b;
vector_a.reserve(10); // this works in all tested compilers
vector_b.reserve(10); // this only works in clang
我的问题是,这是否是由于标准的不完整实施造成的,或者如果它只是没有定义(故意的?)。
我测试了一些标准容器:
#include <iostream>
#include <deque>
#include <vector>
#include <queue>
#include <stack>
int main() {
std::cout << "Deque: " << std::is_nothrow_move_constructible<std::deque<float>>::value << std::endl;
std::cout << "Vector: " << std::is_nothrow_move_constructible<std::vector<float>>::value << std::endl;
std::cout << "Queue: " << std::is_nothrow_move_constructible<std::queue<float>>::value << std::endl;
std::cout << "Stack: " << std::is_nothrow_move_constructible<std::stack<float>>::value << std::endl;
}
通用条款7.2.1:
Deque: 0
Vector: 1
Queue: 0
Stack: 0
clang 5.0.0:
Deque: 1
Vector: 1
Queue: 1
Stack: 1
适用于x64的Microsoft C/C++19.00.23506版本:
德克:0
矢量:1
队列:0
堆栈:0
编辑
使用向量作为基础容器的队列和堆栈的结果:
std::cout << "Vector Stack: " << std::is_nothrow_move_constructible<std::stack<float, std::vector<float>>>::value << std::endl;
std::cout << "Vector Queue: " << std::is_nothrow_move_constructible<std::queue<float, std::vector<float>>>::value << std::endl;
通用条款7.2.1:
Vector Stack: 1
Vector Queue: 1
clang 5.0.0:
向量堆栈:1
向量队列:1
适用于x64的Microsoft C/C++19.00.23506版本:
向量堆栈:1
向量队列:1