向量必须与其分配器具有相同的值
vector must have the same value as its allocator
这个 MCVE compiles/runs 与 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";
}
其中 Allocator
是我自己的分配器。
我们现在使用 gcc 8.2 并遇到此错误
FaF::smatch results {};
^--- vector must have the same value as its allocator
当我将 FaF::smatch
更改为默认值 std::smatch
时,它会 compiles/runs 与 gcc 8.2。
我的问题:
为什么这个代码不能用 gcc 8.2 编译,即使它用 gcc 7.3 和 C++17 设置编译 - 没有其他改变。这就是让我困惑的地方。某处改变了一些显然与 C++ 无关的东西。
看到它 live - clang 6.0 也接受带有 FaF::smatch
的版本。
编译器标志:
-O3 -std=c++17 -Werror -Wextra -Wold-style-cast -Wall
我在 gnu gcc bug database 提交了这个案例,解决方案是这样的:
using smatch = std::match_results<FaF::string::const_iterator,
Allocator<std::sub_match<FaF::string::const_iterator>>>;
^^^^^^^^^^^^^^^
这里是来自 gnu 错误数据库的答案 link:
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++.
我要感谢 gnu 团队的快速回复,这对 SO 社区也有帮助!
这个 MCVE compiles/runs 与 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";
}
其中 Allocator
是我自己的分配器。
我们现在使用 gcc 8.2 并遇到此错误
FaF::smatch results {};
^--- vector must have the same value as its allocator
当我将 FaF::smatch
更改为默认值 std::smatch
时,它会 compiles/runs 与 gcc 8.2。
我的问题:
为什么这个代码不能用 gcc 8.2 编译,即使它用 gcc 7.3 和 C++17 设置编译 - 没有其他改变。这就是让我困惑的地方。某处改变了一些显然与 C++ 无关的东西。
看到它 live - clang 6.0 也接受带有 FaF::smatch
的版本。
编译器标志:
-O3 -std=c++17 -Werror -Wextra -Wold-style-cast -Wall
我在 gnu gcc bug database 提交了这个案例,解决方案是这样的:
using smatch = std::match_results<FaF::string::const_iterator,
Allocator<std::sub_match<FaF::string::const_iterator>>>;
^^^^^^^^^^^^^^^
这里是来自 gnu 错误数据库的答案 link:
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++.
我要感谢 gnu 团队的快速回复,这对 SO 社区也有帮助!