移动构造函数是否有容器内实现定义的
我的问题是,这是否是由于标准的不完整实现造成的,还是故意没有定义?
我测试了一些标准容器:
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;
}
gcc 7.2.1:
Deque: 0
Vector: 1
Queue: 0
Stack: 0
clang 5.0.0:
Deque: 1
Vector: 1
Queue: 1
Stack: 1
Microsoft C/C++ 版本 19.00.23506(x64):
Deque: 0
Vector: 1
Queue: 0
Stack: 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;
gcc 7.2.1:
Vector Stack: 1
Vector Queue: 1
clang 5.0.0:
Vector Stack: 1
Vector Queue: 1
Microsoft C/C++版本19.00.23506适用于x64:
Vector Stack: 1
Vector Queue: 1