将Generic Lambda与std::find_if一起使用
Use Generic Lambda with std::find_if
我的编译器是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>
的变体(例如const
、const 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编译(除非有一些警告)。您的问题不在于发布的代码,而在于其他地方。
将来,请发布实际的错误消息,并将您发布的代码实际键入编译器,并观察编译器生成所述错误。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- 我如何将 OR 与 if 语句一起使用字符串
- 如何在 C++17 中将 "else-if" 与初始值设定项一起使用?
- 将 if else 与开关一起使用
- typedef的替代品或将typedef与if-else一起使用的正确方法
- 如何将用户输入与if语句组合在一起
- 我可以在 IF 语句中将比较链接在一起吗?
- 将 if 语句与指针(BST 树)一起使用时出现分段错误