简体   繁体   English

使用 `std::max_element` `error: void value not ignored as it should be`

[英]`error: void value not ignored as it ought to be` using `std::max_element`

I was trying to get the element with largest size from a vector of elements, as succinct as possible.我试图从元素向量中获取最大尺寸的元素,尽可能简洁。 The original code looks like,原始代码看起来像,

std::cout << ((*std::max_element(rooms.begin(), rooms.end(),
          [](auto a, auto b){a.size() < b.size();})).size()) << std::endl;

which g++ complains, g++抱怨,

wall3.cpp: In function ‘int main()’:
wall3.cpp:150:47: error: ‘class __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >’ has no member named ‘size’
  150 |     [](auto a, auto b){a.size() < b.size();}).size() << std::endl;
      |                                               ^~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/string:40,
                 from /usr/include/c++/9/bitset:47,
                 from wall3.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h: In instantiation of ‘constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = main()::<lambda(auto:1, auto:2)>]’:
/usr/include/c++/9/bits/stl_algo.h:5692:12:   required from ‘constexpr _ForwardIterator std::__max_element(_ForwardIterator, _ForwardIterator, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1, auto:2)> >]’
/usr/include/c++/9/bits/stl_algo.h:5743:43:   required from ‘constexpr _FIter std::max_element(_FIter, _FIter, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = main()::<lambda(auto:1, auto:2)>]’
wall3.cpp:150:45:   required from here
/usr/include/c++/9/bits/predefined_ops.h:143:18: error: void value not ignored as it ought to be
  143 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It turns out that I forgot to use return in the lambda expression.原来我忘记在 lambda 表达式中使用return Adding return solved the problem.添加return解决了这个问题。

Lesson: use a different compiler and turn on the strictest warning configurations to reveal the problem faster.教训:使用不同的编译器并打开最严格的警告配置以更快地发现问题。 The output from clang++ is far more explicit,来自clang++的 output 更加明确,

wall3.cpp:150:33: warning: relational comparison result unused [-Wunused-comparison]
                  [](auto a, auto b){a.size() < b.size();})).size()) << std::endl;
                                     ~~~~~~~~~^~~~~~~~~~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM