std::用两个双变量交换会导致g++编译器错误



由于未知原因,一个简单的std::swap会导致编译器错误:

export module BoundingBox;
import Vector3d;
... (custom files)
import <list>;
import <string>;
import <cassert>;
import <numbers>;
import <algorithm>;
import <cstddef>;
using std::swap;
using std::iterator;
using std::list;
using std::string;
using std::numbers::pi;
export class BoundingBox {
void test()
{
double a=1, b=2;
std::swap(a,b);
}
};
...

这导致编译器错误:

In file included from /usr/include/c++/11.2.0/bits/stl_pair.h:59,
from /usr/include/c++/11.2.0/bits/stl_algobase.h:64,
from /usr/include/c++/11.2.0/bits/char_traits.h:39,
from /usr/include/c++/11.2.0/string:40,
of module /usr/include/c++/11.2.0/string, imported at raytracer/BoundingBox.cpp:8:
/usr/include/c++/11.2.0/bits/move.h: In instantiation of ‘constexpr std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = double; std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > = void]’:
raytracer/BoundingBox.cpp:1503239:18:   required from here
/usr/include/c++/11.2.0/bits/move.h:204:19: internal compiler error: in tsubst_copy, at cp/pt.c:16621
204 |       _Tp __tmp = _GLIBCXX_MOVE(__a);
|                   ^~~~~~~~~~~~~
0xe4c988 internal_error(char const*, ...)
???:0
0xe42d02 fancy_abort(char const*, int, char const*)
???:0
0x1140ae0 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
???:0
0x1141098 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
???:0
0x1140f06 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
???:0
0x11f00ff tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
???:0
0x11f049b tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
???:0
0x11f01de tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
???:0
0x11f0174 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
???:0
0x1294a36 instantiate_decl(tree_node*, bool, bool)
???:0
0xfe2243 instantiate_pending_templates(int)
???:0
0xfddab7 c_parse_final_cleanups()
???:0

不幸的是,我无法生成一个最低限度的工作示例,因为在我的测试用例中从未发生过错误。然而,我对C++没有经验,甚至不知道我应该在巨大的代码库中寻找什么。是什么原因造成的?我使用g++11.2.0并编译如下:

g++ -c -fmodules-ts -x c++-system-header  -std=c++20 iostream algorithm numbers string vector cmath list cassert fstream cstddef
g++ -fmodules-ts -std=c++20 -lGL -lglut -c 
raytracer/Vector3d.cpp 
...
raytracer/BoundingBox.cpp
...

它也是通过编译utility修复的:

g++ -c -fmodules-ts -x c++-system-header  -std=c++20 iostream algorithm numbers string vector cmath list cassert fstream cstddef utility
g++ -fmodules-ts -std=c++20 -lGL -lglut -c 
raytracer/Vector3d.cpp 
...
raytracer/BoundingBox.cpp
...

奇怪的是,在将import <algorithm>切换为import <utility>之后,即使我将其切换回算法或删除两者,它也能工作。

最新更新