此 MCVE 使用 gcc 7.3 编译/运行:
请注意,此 MCVE 已显着减少以保持错误重现,因此Allocator模板中的代码没有意义,但不会影响星座!
#include <regex>
#include <string>
#include <iostream>
namespace FaF
{
template <typename T>
class Allocator
{
public:
typedef T value_type;
Allocator() throw() {}
template <typename U> Allocator (const Allocator<U>&) throw() {}
~Allocator() throw() {}
T* allocate (std::size_t num, const void* hint = 0)
{
(void) hint; (void) num;
return new ( T );
}
void deallocate (T* p, std::size_t num) { (void) num; (void) p; }
};
using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
using smatch = std::match_results<FaF::string::const_iterator, Allocator<FaF::string::const_iterator>>;
}
int main()
{
FaF::smatch results {};
std::cout << "OK\n";
}
Run Code Online (Sandbox Code Playgroud)
Allocator我自己的分配器在哪里。
我们现在使用 gcc 8.2 并收到此错误
FaF::smatch results {};
^--- vector must have the same value as its allocator
Run Code Online (Sandbox Code Playgroud)
当我更改FaF::smatch为默认值时,std::smatch它会使用 gcc 8.2 编译/运行。
我的问题:
是什么原因以及为什么这段代码不能用 gcc 8.2 编译,即使它用带有 C++17 设置的 gcc 7.3 编译 - 没有其他任何改变。这就是让我困惑的地方。某处改变了一些显然与 C++ 无关的东西。
现场观看- clang 6.0 也接受该版本FaF::smatch。
编译器标志:
-O3 -std=c++17 -Werror -Wextra -Wold-style-cast -Wall
我在gnu gcc 错误数据库中提交了这个案例,解决方案是这样的:
using smatch = std::match_results<FaF::string::const_iterator,
Allocator<std::sub_match<FaF::string::const_iterator>>>;
^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
这是来自 gnu bug 数据库链接的答案:
The value type of match_result<Iter> is sub_match<Iter>,
so you need to use Allocator<sub_match<Iter>> not Allocator<Iter>.
> Changing it to std::smatch then it compiles.
Because that uses the correct allocator type.
> Compiler options:
> -O3 -std=c++17 -Werror -Wextra -Wold-style-cast -Wall
If you use -std=gnu++17 then your code will be accepted,
but is not portable and is not valid C++.
Run Code Online (Sandbox Code Playgroud)
我要感谢 gnu 团队的快速回复,这对 SO 社区也有帮助!
| 归档时间: |
|
| 查看次数: |
989 次 |
| 最近记录: |