c++ - vector 必须与其分配器具有相同的值

标签 c++ c++17 gcc8

此 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";
}

其中 Allocator 是我自己的分配器。

我们现在使用 gcc 8.2 并得到这个错误

    FaF::smatch results {};
    ^--- vector must have the same value as its allocator

当我将 FaF::smatch 更改为默认的 std::smatch 时,它会使用 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 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++.

我要感谢 gnu 团队的快速回复,这对 SO 社区也有帮助!

关于c++ - vector 必须与其分配器具有相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51920293/

相关文章:

c - gcc-8 Wstringop-截断

c++ - 使用 ffmpeg (c++) 编码 AAC

c++ - 类类型重定义 C++

c++模板参数的自动模板推导失败

c++ - 依赖于 char 符号的专用结构模板

c++ - 如何根据排序索引 vector 对 std::set 索引进行排序?

c++ - Google Sparsehash 在不可平凡复制的类型上使用 realloc()

c++ - 不需要删除动态数据?

c++ - C++ 中的多态性和虚函数

c++ - emplace_back 导致静态 constexpr 成员上的链接错误