направляющие вычета std :: set не работают так, как я ожидал - PullRequest
3 голосов
/ 30 сентября 2019

Я бы ожидал, что руководство по выводу правильно выведет тип из приведенного ниже примера, но они не :

#include <set>

struct Foo { };

bool cmp(const Foo&, const Foo& );

std::set my_set({Foo{}, Foo{}}, cmp);

Ошибка компилятора (оба gcc / clang показывают похожую диагностику):

In file included from /opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from /opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/bits/allocator.h:46,
                 from /opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/bits/stl_tree.h:64,
                 from /opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/set:60,

                 from <source>:1:

/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<bool(const Foo&, const Foo&)>':
/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/bits/alloc_traits.h:634:11:   recursively required by substitution of 'template<class _Alloc> struct std::__is_allocator<_Alloc, std::__void_t<typename _Alloc::value_type, decltype (declval<_Alloc&>().allocate(long unsigned int{}))> > [with _Alloc = std::allocator<bool(const Foo&, const Foo&)>]'
/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/bits/alloc_traits.h:634:11:   required by substitution of 'template<class _Alloc> using _RequireAllocator = typename std::enable_if<std::__is_allocator<_Alloc>::value, _Alloc>::type [with _Alloc = std::allocator<bool(const Foo&, const Foo&)>]'
/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/bits/stl_set.h:938:5:   required by substitution of 'template<class _InputIterator, class _Compare, class _Allocator, class, class, class> std::set(_InputIterator, _InputIterator, _Compare, _Allocator)-> std::set<typename std::iterator_traits<_Iter>::value_type, _Compare, _Allocator> [with _InputIterator = bool (*)(const Foo&, const Foo&); _Compare = std::less<bool(const Foo&, const Foo&)>; _Allocator = std::allocator<bool(const Foo&, const Foo&)>; <template-parameter-1-4> = void; <template-parameter-1-5> = std::less<bool(const Foo&, const Foo&)>; <template-parameter-1-6> = <missing>]'

<source>:7:36:   required from here

/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/ext/new_allocator.h:96:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = bool(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::const_pointer = bool (*)(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::const_reference = bool (&)(const Foo&, const Foo&)]' cannot be overloaded with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = bool(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::pointer = bool (*)(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::reference = bool (&)(const Foo&, const Foo&)]'

   96 |       address(const_reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~

/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/ext/new_allocator.h:92:7: note: previous declaration '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = bool(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::pointer = bool (*)(const Foo&, const Foo&); __gnu_cxx::new_allocator<_Tp>::reference = bool (&)(const Foo&, const Foo&)]'

   92 |       address(reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~

Compiler returned: 1

Что, по-моему, намекает на то, что компилятор не использует конструктор, принимающий список инициализаторов, а вместо этого пытается трактовать его как версию с 2 итераторами (возможно, я не правильно его читаю, конечно).

Что может быть причиной этого? Я неправильно читаю руководства по дедукции или это что-то еще?

1 Ответ

3 голосов
/ 30 сентября 2019

Вероятно, это ошибка компилятора / библиотеки, так как маленький вариант работает:

  • В явном виде о initializer_list ( Демо ):

    std::set my_set(std::initializer_list<Foo>{Foo{}, Foo{}}, cmp);
    
  • добавление распределителя ( Демо ):

    std::set my_set({Foo{}, Foo{}}, cmp, std::allocator<Foo>{});
    
  • с использованием Functor ( Демо ):

    struct MyComparer
    {
        bool operator()(const Foo&, const Foo&) const;
    };
    
    std::set my_set({Foo{}, Foo{}}, MyComparer{});
    
  • И изменение библиотеки (libc ++ и libstdc ++) дает разные результаты Демо для дополнительного варианта распределителя.

...