std::map::reverse_iterator 与不完整类型一起使用时不适用于 C++20



我注意到以下示例中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以来,vectorlistforward_list除外(。请参见此处。因此,它可能工作,也可能在任何时候停止工作,但基本上任何事情都可能发生,不应该依赖它

如果能够存储不完整的类型对于您的用例来说是一个硬性要求,那么您可能需要检查boost::container

最新更新