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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:03:53+00:00 2026-06-14T21:03:53+00:00

How should I pass a function inside an struct as a functor? I assumed

  • 0

How should I pass a function inside an struct as a functor? I assumed this should work fine, but it didn’t:

#include <algorithm>
using namespace std;

struct s {
    int a[10];

    bool cmp(int i, int j) {
        // return something
    }

    void init() {
        sort(a, a + 10, cmp);
    }
};

which gets <unresolved overloaded function type>

  • 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-14T21:03:54+00:00Added an answer on June 14, 2026 at 9:03 pm

    You cannot do this directly, because cmp is a member function, which requires three arguments: i, j, and the invisible, implicit this pointer.

    To pass cmp to std::sort, make it a static function, which does not belong to any particular instance of s and thus doesn’t have a this pointer:

    static bool cmp(int i, int j) {
        // return something
    }
    

    If you need access to this, you can wrap cmp in a simple function object instead:

    struct cmp {
        s &self;
        cmp(s &self) : self(self) { }
        bool operator()(int i, int j) {
            // return something, using self in the place of this
        }
    };
    

    And call it like this:

    sort(a, a + 10, cmp(*this));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the following step pass: page.has_css?(doesnt exist) when this correctly fails: page.should have_css(doesnt
If only string is allowed to pass into function process(), what should we do
I need to pass the contents of an inside div to a function, for
Should I always pass a std::string by const reference to a function if all
I have android app . i should pass the data from android to web
Suppose I want to reflect exception in the log. Should I pass stack trace
I have simple win service, that executes few tasks periodically. How should I pass
Should I access the asp.net membership class from the controller and pass the results
My logic is if( !this.draginited() ) // a drag-disabled element shouldn't get pass here,
I need help with malloc() inside another function . I'm passing a pointer and

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.