将Generic Lambda与std::find_if一起使用

Use Generic Lambda with std::find_if

本文关键字:if 一起 find Lambda std Generic      更新时间:2023-10-16

我的编译器是gcc-4.9,我使用-std=c++14进行编译。我有以下代码,它不可编译。

 set<int> pal;
 set<tuple<int, int, int>> pal_group;
 // ... populate pal & pal_group
 auto itr_pal = max_element(pal.begin(), pal.end());
 auto itr_pal_group =
     find_if(
         pal_group.begin(), pal_group.end(), [&itr_pal] (auto pal_tuple) {
             return true;
         }
     );

然而,一旦我开始实际使用pal_tuple,上面的代码就会编译。例如

     find_if(
         pal_group.begin(), pal_group.end(), [&itr_pal] (auto pal_tuple) {
             int pal;
             tie(pal, ignore, ignore) = pal_tuple;
             return pal == *itr_pal;
         }
     );

我的怀疑是,如果我没有像tuple<int, int, int>那样实际使用它,编译器就无法计算出pal_tuple的类型。然而,一元谓词的原型似乎表明pal_tuple的类型只能是tuple<int, int, int>的变体(例如constconst T&等)?

以下内容来自cppreference.com:

The signature of the predicate function should be equivalent to the following:
    bool pred(const Type &a);
The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

我想知道为什么编译器在没有我实际使用pal_tuple的情况下无法计算出它的类型。

谢谢,

编辑:这是一个错误。。。所以编译器实际上只是给我警告,但我认为这是一个错误。我以后在发布问题之前会更加小心。。。

g++-4.9 -std=c++14 -Wall -Wextra p4.cpp -o p4.exec
p4.cpp: In instantiation of 'main()::<lambda(auto:1)> [with auto:1 = std::tuple<int, int, int>]':
/usr/local/Cellar/gcc49/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/bits/predefined_ops.h:231:30:   required from 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_const_iterator<std::tuple<int, int, int> >; _Predicate = main()::<lambda(auto:1)>]'
/usr/local/Cellar/gcc49/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/bits/stl_algo.h:104:50:   required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator<std::tuple<int, int, int> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(auto:1)> >]'
/usr/local/Cellar/gcc49/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/bits/stl_algo.h:162:43:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_const_iterator<std::tuple<int, int, int> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(auto:1)> >]'
/usr/local/Cellar/gcc49/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/bits/stl_algo.h:3804:45:   required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_const_iterator<std::tuple<int, int, int> >; _Predicate = main()::<lambda(auto:1)>]'
p4.cpp:49:9:   required from here
p4.cpp:41:66: warning: unused parameter 'pal_tuple' [-Wunused-parameter]
             pal_group.begin(), pal_group.end(), [&itr_pal] (auto pal_tuple) {
                                                                  ^

您的代码是有效的,并且在我尝试时使用gcc 4.9编译(除非有一些警告)。您的问题不在于发布的代码,而在于其他地方。

将来,请发布实际的错误消息,并将您发布的代码实际键入编译器,并观察编译器生成所述错误。