Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:06:56+00:00 2026-05-14T01:06:56+00:00

I have some complex types here so I decided to use nifty trick to

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:06:56+00:00Added an answer on May 14, 2026 at 1:06 am

    Two issues.

    The thing that you are going after (element<T>::value) is not a typename.

    However, firstly you’ll need nested binds: one to access _1.first and another one to access the value of the previous.

    Without the typedefs:

      std::find_if(
        container.begin(), container.end(),
        bind(
          &element<T>::value,
          bind(&std::pair<element<T>, std::vector<T> >::first, boost::lambda::_1)
        ) == value
      ); 
    

    And so, with your idiom, without the unnecessary typename:

      std::find_if( container.begin(), container.end(),
        bind(
          &vector_containter<T>::type::value_type::first_type::value,
          bind(
             &vector_containter<T>::type::value_type::first, boost::lambda::_1)
          ) == value
      );
    

    This doesn’t seem particularly readable, though. Perhaps a) wait for C++0x lambda, b) write a specific functor for the find_if call, c) just do a manual loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some XSDs in our project that define types that use attributes, as
I have some complex stored procedures that may return many thousands of rows, and
I have some UI in VB 2005 that looks great in XP Style, but
I have some ASP.NET web services which all share a common helper class they
We have some input data that sometimes appears with &nbsp characters on the end.
I have some classes layed out like this class A { public virtual void
We have some files on our website that users of our software can download.
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some C# / asp.net code I inherited which has a textbox which
I have some code like this in a winforms app I was writing to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.