#include <typeinfo>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <boost/shared_ptr.hpp>
#include <boost/mem_fn.hpp>
#include <boost/functional.hpp>
class Item_t
{
private:
int m_ival;
public:
typedef boost::shared_ptr<Item_t> sptr_t;
int GetIval() { return m_ival; }
Item_t(int ival):m_ival(ival) {}
template<int ival> bool IvalLt() { return m_ival < ival; }
void print() { std::cout << m_ival << std::endl; }
template<int ival> static bool st_IvalLt(Item_t::sptr_t sptr) { return sptr->GetIval() < ival; }
};
int main(void)
{
std::vector<Item_t::sptr_t> items;
std::vector<Item_t::sptr_t>::iterator el;
items.push_back(Item_t::sptr_t(new Item_t(10)));
items.push_back(Item_t::sptr_t(new Item_t(5)));
items.push_back(Item_t::sptr_t(new Item_t(30)));
items.push_back(Item_t::sptr_t(new Item_t(20)));
// работает
el = find_if(items.begin(), items.end(), boost::mem_fn(&Item_t::IvalLt<7>));
if(el != items.end())
(*el)->print();
// не компилируется
el = find_if( items.begin(), items.end(), std::not1(boost::mem_fn(&Item_t::IvalLt<25>)) );
if(el != items.end())
(*el)->print();
// работает
el = find_if( items.begin(), items.end(), std::not1(std::ptr_fun(&Item_t::st_IvalLt<30>)) );
if(el != items.end())
(*el)->print();
std::vector<Item_t*> items_p;
items_p.push_back(new Item_t(5));
items_p.push_back(new Item_t(10));
// работает
std::vector<Item_t*>::iterator el_p =
find_if( items_p.begin(), items_p.end(), std::not1(boost::mem_fn(&Item_t::IvalLt<7>)) );
if(el_p != items_p.end())
(*el_p)->print();
}
Пишет:
stl_algo.h:259: error: no match for call to
`(std::unary_negate<boost::_mfi::mf0<bool, Item_t> >)
(boost::shared_ptr<Item_t>&)'
stl_function.h:323: note: candidates are: bool
std::unary_negate<_Predicate>::operator()(const typename
_Predicate::argument_type&) const [with _Predicate =
boost::_mfi::mf0<bool, Item_t>]
Если написать, boost::not1, то тоже не компилируется.
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
Похожие темы
- Форум C++, std::function, шаблоны, что я делаю не так? (2013)
- Форум boost::spirit и CharT != char (2005)
- Форум [C++, boost::spirit::phoenix, std::get] Обернуть std::get в phoenix-овскую function (2011)
- Форум Стиль или как правильно (2014)
- Форум vectror<const A*> (2015)
- Форум lambda vs std::function<...> (2014)
- Форум Различия между boost::enable_if и std::enable_if (2015)
- Форум boost::multi_index_container<const int> (2013)
- Форум boost.asio, boost.serialization (2010)
- Форум [boost] проблемы с bind (2010)