Extracting pair member in lambda expressions and template typedef idiom

Posted by Nazgob on Stack Overflow See other posts from Stack Overflow or by Nazgob
Published on 2010-03-22T23:23:11Z Indexed on 2010/03/23 0:11 UTC
Read the original article Hit count: 709

Filed under:
|
|
|
|

Hi, I have some complex types here so I decided to use nifty trick to have typedef on templated types. Then I have a class some_container that has a container as a member. Container is a vector of pairs composed of element and vector. I want to write std::find_if algorithm with lambda expression to find element that have certain value. To get the value I have to call first on pair and then get value from element. Below my std::find_if there is normal loop that does the trick. My lambda fails to compile. How to access value inside element which is inside pair? I use g++ 4.4+ and VS 2010 and I want to stick to boost lambda for now.

#include <vector>
#include <algorithm>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\bind.hpp>

template<typename T>
class element
{
public:
   T value;
};

template<typename T>
class element_vector_pair // idiom to have templated typedef
{
public:
   typedef std::pair<element<T>, std::vector<T> > type;
};

template<typename T>
class vector_containter // idiom to have templated typedef
{
public:
   typedef std::vector<typename element_vector_pair<T>::type > type;
};

template<typename T>
bool operator==(const typename element_vector_pair<T>::type & lhs, const typename element_vector_pair<T>::type & rhs)
{
   return lhs.first.value == rhs.first.value;
}

template<typename T>
class some_container
{
public:
   element<T> get_element(const T& value) const
   {
      std::find_if(container.begin(), container.end(), bind(&typename vector_containter<T>::type::value_type::first::value, boost::lambda::_1) == value);
      /*for(size_t i = 0; i < container.size(); ++i)
      {
      if(container.at(i).first.value == value)
      {
      return container.at(i);
      }
      }*/
      return element<T>(); //whatever
   }

protected:
   typename vector_containter<T>::type container;
};

int main()
{
   some_container<int> s;
   s.get_element(5);
   return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about lambda