[英]Is this a bug in gcc 11.2.1 related to _GLIBCXX_USE_CXX11_ABI?
The following C++ code fails to compile on gcc 11.2.1 on CentOS 7.2.2009 (available through Software Collections only) using the following command:
命令: scl enable devtoolset-11 'g++ -std=c++11 test.cpp -o test'
代码:
#include <iostream>
#include <string>
int main() {
std::string s="abcdefg";
s.erase(s.cbegin(), s.cend());
return 0;
}
错误:
test.cpp: In function 'int main()':
test.cpp:7:16: error: no matching function for call to 'std::basic_string<char>::erase(std::basic_string<char>::const_iterator, std::basic_string<char>::const_iterator)'
7 | s.erase(s.cbegin(), s.cend());
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/rh/devtoolset-11/root/usr/include/c++/11/string:55,
from /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/locale_classes.h:40,
from /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/ios_base.h:41,
from /opt/rh/devtoolset-11/root/usr/include/c++/11/ios:42,
from /opt/rh/devtoolset-11/root/usr/include/c++/11/ostream:38,
from /opt/rh/devtoolset-11/root/usr/include/c++/11/iostream:39,
from test.cpp:1:
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4766:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]'
4766 | erase(size_type __pos = 0, size_type __n = npos)
| ^~~~~
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4766:23: note: no known conversion for argument 1 from 'std::basic_string<char>::const_iterator' to 'std::basic_string<char>::size_type' {aka 'long unsigned int'}
4766 | erase(size_type __pos = 0, size_type __n = npos)
| ~~~~~~~~~~^~~~~~~~~
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4783:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = std::basic_string<char>::iterator]'
4783 | erase(iterator __position)
| ^~~~~
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4783:7: note: candidate expects 1 argument, 2 provided
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4803:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = std::basic_string<char>::iterator]'
4803 | erase(iterator __first, iterator __last);
| ^~~~~
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4803:22: note: no known conversion for argument 1 from '__normal_iterator<const char*,[...]>' to '__normal_iterator<char*,[...]>'
4803 | erase(iterator __first, iterator __last);
| ~~~~~~~~~^~~~~~~
该错误意味着编译器无法找到方法std::string::erase(const_iterator, const_iterator)
,该方法应该在 C++11 中可用(根据此参考)。
相同的代码在使用相同版本的 gcc 的其他操作系统上编译良好(例如这里)
我能够找到导致 CentOS 与其他发行版中编译器宏_GLIBCXX_USE_CXX11_ABI
定义的行为差异的原因。 大多数发行版启用_GLIBCXX_USE_CXX11_ABI
而 CentOS 强制禁用它,如此处所述。 在此处查看有关宏的更多信息。
我目前对宏_GLIBCXX_USE_CXX11_ABI
的理解是这样的:
std::basic_string
的 ABI 发生了变化。std::basic_string::erase(const_iterator, const_iterator)
都应该可用。 但是,我观察到的是方法std::basic_string::erase(const_iterator, const_iterator)
在较旧的 ABI 中根本不可用(即当_GLIBCXX_USE_CXX11_ABI=0
时。
这是 gcc 中的错误,还是我对宏_GLIBCXX_USE_CXX11_ABI
的理解有问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.