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

  • SEARCH
  • Home
  • 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 7923643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:32+00:00 2026-06-03T17:23:32+00:00

I have a vector of smart ptr of my class Foo: struct Foo {

  • 0

I have a vector of smart ptr of my class Foo:

struct Foo
{
  Foo() : mEnabled( false ) {}

  bool mEnabled;

  bool isEnabled() const { return mEnabled; }
  void setEnabled( bool inEnabled ) { mEnabled = inEnabled; }
  /* ... */
};

typedef std::tr1::shared_ptr< Foo > tFooPtr;

typedef std::vector< tFooPtr > tFooVec;

I have this working nicely:

 tFooVec foo_vector; // insert couple of elements
 size_t count = count_if( foo_vector.begin(), foo_vector.end(), std::tr1::mem_fn( &Foo::isEnabled ) );

But what functional “helper” to use when I want to count_if the “disabled” Foo objects

 size_t count = count_if( foo_vector.begin(), foo_vector.end(), std::not1( std::tr1::mem_fn( &Foo::isEnabled ) ) ); // does not compile

above line does not compile:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:446: error: no match for call to '(std::unary_negate<std::tr1::_Mem_fn<bool (Foo::*)()const> >) (std::tr1::shared_ptr<Foo>&)'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:322: note: candidates are: bool std::unary_negate<_Predicate>::operator()(const typename _Predicate::argument_type&) const [with _Predicate = std::tr1::_Mem_fn<bool (Foo::*)()const>]
make: *** [src/shared_ptr_tests.o] Error 1

(Using g++ 4.1.2 on Linux)

I think the compile problem comes from the fact that std::not1 is using std::unary_negate which requires that the function / Predicate provided Predicate::argument_type . The latter is given when the Predicate is derived from std::unary_function sigh

Having said this I assume that std::tr1::mem_fn is not using std::unary_function nor providing the argument_type.

The solution I am now using is, that I am now using boost::bind instead of std::tr1::bind

#include <boost/bind.hpp>
using namespace boost;
...
size_t countboost = count_if( foo_vector.begin(), foo_vector.end(), !( bind( &Foo::isEnabled, _1 )) );

To avoid complications (and confusion), I replacde the usage of std::tr1::bind with boost::bind throughout my code.

  • 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-06-03T17:23:34+00:00Added an answer on June 3, 2026 at 5:23 pm

    !boost::bind(… works for me:

      bool test(int i)
      {
        return i < 2;
      }
    
      TEST( boost_bind_test, negateTest )
      {
        std::vector<int> vec;
        vec.push_back(1);
        vec.push_back(2);
        vec.push_back(3);
    
        ASSERT_EQ(2, count_if( vec.begin(), vec.end(), !boost::bind(&test, _1)));
      };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have thousands of struct or class objects in a vector, how to
I have a vector of pointers to a class. I need to call their
I'd like to have a vector of unique_ptr's as a member of a class
I have the following code snippet: std::vector< boost::shared_ptr<Foo> >::iterator it; it = returnsAnIterator(); //
Would it be smart to have a vector in an object with a list
I have a vector class with hashCode() implemented. It wasn't written by me, but
Let's say I have a: std::vector<std::list<Node> > And let's say Node is: class Node
Say I have a simple struct that contains a vector and defines a copy
I have a vector with raw pointers (no, I cannot use smart pointers) and
Given the following: struct Foo { int bar() const; }; struct IsEqual : public

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.