我注意到以下示例中std::map::reverse_iterator
的使用不适用于C++20,但在所有编译器中都适用于C++17。
演示
演示MSVC
#include <map>
class C; //incomplete type
class Something
{
//THIS WORKS IN C++17 as well as C++20 in all compilers
std::map<int, C>::iterator obj1;
//THIS DOESN'T WORK in C++20 in all compilers but works in C++17 in all compilers
std::map<int, C>::reverse_iterator obj2;
};
int main()
{
Something s;
return 0;
}
我的问题是C++20
发生了什么变化,以至于所有C++20
编译器都停止使用std::map::reverse_iterator
。
它在C++20之前偶然工作,按照标准,在std容器中使用不完整类型是UB(自C++17以来,vector
、list
和forward_list
除外(。请参见此处。因此,它可能工作,也可能在任何时候停止工作,但基本上任何事情都可能发生,不应该依赖它
如果能够存储不完整的类型对于您的用例来说是一个硬性要求,那么您可能需要检查boost::container
。