代码之家  ›  专栏  ›  技术社区  ›  Peter VARGA

向量的值必须与其分配器的值相同

  •  3
  • Peter VARGA  · 技术社区  · 7 年前

    此MCVE与gcc 7.3一起编译/运行:
    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";
    }
    

    哪里 分配器 是我自己的分配器。

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

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

    当我改变 FaF::smatch std::smatch 然后使用gcc 8.2编译/运行。

    我的问题是:

    看看吧 live -clang 6.0接受带有 也。

    编译器标志:
    -O3 -std=c++17 -Werror -Wextra -Wold-style-cast -Wall

    1 回复  |  直到 7 年前
        1
  •  4
  •   Peter VARGA    7 年前

    我在 gnu gcc bug database

    using smatch = std::match_results<FaF::string::const_iterator, 
          Allocator<std::sub_match<FaF::string::const_iterator>>>;
                    ^^^^^^^^^^^^^^^
    

    这里是gnu错误数据库链接的答案:

    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++.
    

    推荐文章