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 3600982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:33:51+00:00 2026-05-18T20:33:51+00:00

Given the following: struct Foo { int bar() const; }; struct IsEqual : public

  • 0

Given the following:

struct Foo
{
    int bar() const;
};

struct IsEqual : public std::unary_function<Foo*, bool>
{
    int val;
    IsEqual(int v) : val(v) {}

    bool operator()(const Foo* elem) const
    {
        return elem->bar() == val;
    }
};

I have a container of Foo* and I use std::find_if and std::not1 to find out if there are any elements in the container where bar() returns something different from a given value. The code looks like this:

// Are all elements equal to '2'?
bool isAllEqual(const std::vector<Foo*> &vec)
{
    return find_if(vec.begin(), vec.end(), std::not1(IsEqual(2))) == vec.end();
}

Fast-forward into the future and I now have a different container, this time containing std::tr1::shared_ptr<Foo>. I’d love to simply re-use my functor in an overloaded version of isAllEqual(). But I can’t. Foo* and shared_ptr<Foo> are different types. And I need to inherit from unary_function so I can use not1. It’d be more elegant if I could avoid writing the same functor twice.

Questions:

  • Is there any way to write IsEqual so it can use both raw and smart pointers?
  • Did I handcuff myself by using std::not1? Should I just write IsNotEqual instead?

Restrictions:

  1. I can’t use anything from the boost library.
  2. Our compiler isn’t cool enough to support C++0x lambdas.
  • 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-18T20:33:52+00:00Added an answer on May 18, 2026 at 8:33 pm
    // --*-- C++ --*--
    
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    // Template unary function example.
    template <typename T>
    struct IsEqual : public std::unary_function<T, bool>
    {
        int v;
    
        IsEqual (int v) : v (v) {}
    
        bool operator () (const T & elem) const
        {
            return elem ? elem->bar () == v : false;
        }
    };
    
    // Generic algorithm implementation example...
    template <typename T1, typename T2>
    bool isAllEqual (const T1 & c, T2 v)
    {
        return find_if (
            c.begin (), c.end (),
            std::not1 (IsEqual <typename T1::value_type> (v))) == c.end ();
    }
    
    // Some arbitrary pointer wrapper implementation,
    // provided just for an example, not to include any
    // specific smart pointer implementation.
    template <typename T>
    class WrappedPtr
    {
        const T *v;
    
    public:
        typedef void (WrappedPtr<T>::*unspecified_boolean_type) () const;
    
        WrappedPtr (const T *v) : v (v) {}
    
        const T *operator -> () const { return v; }
    
        operator unspecified_boolean_type () const
        {
            return v != NULL ?
                &WrappedPtr<T>::unspecified_boolean_true : NULL;
        }
    
    private:
        void unspecified_boolean_true () const {}
    };
    
    // Example of structure that could be used with our algorithm.
    struct Foo
    {
        int v;
    
        Foo (int v) : v (v) {}
    
        int bar () const
        {
            return v;
        }
    };
    
    // Usage examples...
    int main ()
    {
        Foo f1 (2), f2 (2);
    
        // Example of using raw pointers...
        {
            std::vector<Foo *> vec;
            vec.push_back (NULL);
            vec.push_back (&f1);
            vec.push_back (&f2);
    
            if (isAllEqual (vec, 2))
                std::cout << "All equal to 2" << std::endl;
            else
                std::cout << "Not all equal to 2" << std::endl;
        }
    
        // Example of using smart pointers...
        {
            std::vector< WrappedPtr<Foo> > vec;
            vec.push_back (NULL);
            vec.push_back (&f1);
            vec.push_back (&f2);
    
            if (isAllEqual (vec, 2))
                std::cout << "All equal to 2" << std::endl;
            else
                std::cout << "Not all equal to 2" << std::endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Given the following: declare @a table ( pkid int, value int ) declare @b
Say i have a data structure like this: type Foo struct { Bar []struct
Given following Ruby statements: (Read input and store each word in array removing spaces
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following XML structure <html> <body> <div> <span>Test: Text2</span> </div> <div> <span>Test: Text3</span>
Given the following: &row->count Would &(row->count) be evaluated or (&row)->count be evaluated in C++?
Given the following code (that doesn't work): while True: # Snip: print out current

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.