首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std:套扣指南不像我预期的那样起作用

std:套扣指南不像我预期的那样起作用
EN

Stack Overflow用户
提问于 2019-09-30 15:15:24
回答 1查看 64关注 0票数 3

我希望在下面的例子中,演绎指南能够正确地推断出类型,但是它们是

代码语言:javascript
复制
#include <set>

struct Foo { };

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

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

编译器错误( gcc/clang都显示类似的诊断):

代码语言:javascript
复制
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迭代器版本(当然,我没有正确地读取它)。

这可能是什么原因?我是误读了演绎指南还是别的什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-30 16:02:55

它可能是一个编译器/库错误,因为很少的变体工作:

  • 明确地了解initializer_list (演示): 设置my_set(std::initializer_list{Foo{},Foo{},cmp);
  • 添加分配器(演示): 设置my_set({Foo{},Foo{},cmp,std::allocator{});
  • 使用函子(演示): 结构MyComparer { bool操作符()( const &,const&) const;};std::set my_set({Foo{},Foo}},MyComparer{});
  • 而变更库(libc++与libstdc++)则给出了附加分配器变量的不同结果( 演示 )。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58170757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档