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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:15:41+00:00 2026-06-11T07:15:41+00:00

In general, templates arguments can be abstract classes, as the program below also shows.

  • 0

In general, templates arguments can be abstract classes, as the program below also shows. But it seems that the compare functor in sort must not be abstract. At least the following does not compile with VC++ 11 and on Oracle Studio 12.

#include <vector>
#include <algorithm>


class Functor
{
public:
    virtual bool operator()(int a, int b) const = 0;
};


class MyFunctor: public Functor
{
public:
    virtual bool operator()(int a, int b) const { return true; }
};


int _tmain(int argc, _TCHAR* argv[])
{
    vector<Functor> fv; // template of abstract class is possible
    vector<int> v;
    MyFunctor* mf = new MyFunctor();
    sort(v.begin(), v.end(), *mf);
    Functor* f = new MyFunctor();
    // following line does not compile: 
    // "Cannot have a parameter of the abstract class Functor"
    sort(v.begin(), v.end(), *f); 
    return 0;
}

Now, I wonder whether this is a general property of functor arguments, or does it depend on the STL implementation? Is there a way to get, what I wanted to do?

  • 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-11T07:15:43+00:00Added an answer on June 11, 2026 at 7:15 am

    Functors generally need to be copyable. Polymorphic base classes are generally not copyable, and abstract bases never.

    Update: Thanks to the comments by @ahenderson and @ltjax, here’s a very simple way to produce a wrapper object that holds your original, polymorphic reference:

    #include <functional>
    
    std::sort(v.begin(), v.end(), std::ref(*f));
    //                            ^^^^^^^^^^^^
    

    The result of std::ref is a std::refrence_wrapper which is exactly what you need: A class with value semantics that holds a reference to your original object.


    The fact that functors get copied throws off lots of people who want to accumulate something inside the functor and then wonder why the results are off. The functor should really take a reference to an external object. To wit:

    Bad! Won’t work as you expect; the functor may get copied arbitrarily:

    struct Func1 {
        int i;
        Func1() : i(0) { }
        void operator()(T const & x) { /* ... */ }
    };
    
    Func1 f;
    MyAlgo(myContainer, f); 
    

    Good: You provide the accumulator; it’s safe to copy the functor:

    struct Func2 {
       int & i;
       Func2(int & n) : i(n) { }
       void operator()(T const & x) { /* ... */ }
    };
    
    int result;
    MyAlgo(myContainer, Func2(result));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm confident I get the general gist of the constructs, but I can't see
I'm new to Razor templates in Umbraco (and in general), but I prefer using
General wisdom is when you remove a component from the stage you also need
General javascript question here, which would also be good to know how(if possible) to
general question is i like to build logger class that writes to single log
I'm very new to jinja2 and the use of templates in general so I
I'm open to persuasion that this is a bad idea. But if not, here's
I was writing some code where I have a class that can accept mixins
I have a lot of code in a big project that has two general
I'll try and make this clear; I've got two classes; GPU(Object) , for general

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.