我的编译器是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
,上面的代码就会编译。
e.g。
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;
}
);
我怀疑编译器无法确定pal_tuple
的类型,而我实际上并不像tuple<int, int, int>
那样使用它。
但是,一元谓词的原型似乎表明pal_tuple
的类型只能是tuple<int, int, int>
的变体(例如const
,const T&
等)?
以下内容来自cpprefernece.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) {
^
答案 0 :(得分:2)
你的代码是有效的,并在我尝试时用gcc 4.9编译(禁止一些警告)。您的问题不在于发布的代码,而在于其他地方。
将来,请发布实际的错误消息,并实际输入您发布到编译器中的代码,并观察编译器生成错误。