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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:45:47+00:00 2026-05-27T15:45:47+00:00

I’m using VS2008 (so I have TR1, but no C++11), and I don’t use

  • 0

I’m using VS2008 (so I have TR1, but no C++11), and I don’t use boost.
The description that follows is a simplified version of my real problem.

I have the following class hierarchy: an interface, an abstract class that implements it and several classes derived from that abstract class:

struct IMyInterface
{
    virtual void MinMax(int& Min, int& Max)const = 0;
};

class Base : public IMyInterface // Abstract
{
// ...
};

class A: public Base
{
public:
    virtual void MinMax(int& Min, int& Max)const { // Some stuff for A}
// ...
};

class B: public Base
{
public:
    virtual void MinMax(int& Min, int& Max)const { // Some stuff for B}
// ...
};

Then I have a vector of shared_ptr to the Base class:

// Somewhere in my code
typedef shared_ptr<Base> Base_sp;

vector<Base_sp> v;

And I have some code that does this:

int Vmin = INT_MAX;
int Vmax = INT_MIN;
for(vector<Base_sp>::const_iterator it = v_sp.begin(); it != v_sp.end(); ++it)
{
    int v1, v2;
    (*it)->MinMax(v1, v2);

    Vmin = std::min(v1, Vmin);
    Vmax = std::max(v2, Vmax);
}

I use similar code in more than one place so I’ve taken it out to a function I run through accumulate:

pair<int, int> MinMaxFinder(pair<int, int> vals, Base_sp elem)
{
    int Vmin = vals.first;
    int Vmax = vals.second;

    if (elem)
    {
        int v1, v2;
        elem->MinMax(v1, v2);

        Vmin = std::min(v1, Vmin);
        Vmax = std::max(v2, Vmax);
    }
    return make_pair(Vmin, Vmax);
}


pair<int, int> tmp = accumulate(v_sp.begin(), v_sp.end(), 
                                make_pair(INT_MAX, INT_MIN), &MinMaxFinder);
int Vmin = tmp.first;
int Vmax = tmp.second;

This works fine, but, in the other places where I use the code I want to replace, I do not necessarily use Base_sp-derived classes, but other IMyInterface-derived classes. So I would like to declare MinMaxFinder like this:

pair<int, int> MinMaxFinder(pair<int, int> vals, IMyInterface* elem)

But that, of course, doesn’t compile.

So, is there a way, through an adapter or something, to do what I want? If not, any ideas on how to tackle this?

  • 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-27T15:45:48+00:00Added an answer on May 27, 2026 at 3:45 pm

    You could parametrise the function over the type of pointer used:

    template <typename MyInterfacePointer>
    pair<int, int> MinMaxFinder(pair<int, int> vals, MyInterfacePointer elem)
    {
        // Function body is identical to yours
        int Vmin = vals.first;
        int Vmax = vals.second;
    
        if (elem)
        {
            int v1, v2;
            elem->MinMax(v1, v2);
    
            Vmin = std::min(v1, Vmin);
            Vmax = std::max(v2, Vmax);
        }
        return make_pair(Vmin, Vmax);
    }
    
    vector<Base_sp> v_sp;   
    accumulate(v_sp.begin(), v_sp.end(), 
               make_pair(INT_MAX, INT_MIN), &MinMaxFinder<Base_sp>);
    
    vector<IMyInterface*> v_i;
    accumulate(v_i.begin(), v_i.end(), 
               make_pair(INT_MAX, INT_MIN), &MinMaxFinder<IMyInterface*>);
    

    For convenience, this could be further wrapped in a function parametrised by the container type:

    template <typename MyInterfacePointerContainer>
    pair<int, int> FindMinMax(MyInterfacePointerContainer const & c)
    {
        return accumulate(c.begin(), c.end(), make_pair(INT_MAX, INT_MIN),
            &MinMaxFinder<typename MyInterfacePointerContainer::value_type>);
    }
    
    vector<Base_sp> v_sp;
    FindMinMax(v_sp);
    
    vector<IMyInterface*> v_i;
    FindMinMax(v_i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.