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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:31:04+00:00 2026-05-28T02:31:04+00:00

Can functors be defined locally within the body of a function? One possible use

  • 0

Can functors be defined locally within the body of a function?

One possible use for locally defined functors I can think of, (especially if we want to use STL algorithms), is say

we have two vectors std::vector<int>‘s a and b then we can consider them in equal in many ways. i.e. a[i] = b[i] (mod loop_counter) where the loop_counter keeps changing and we test them for equality in every loop iteration.

for(int i=0 ; i<=10 ; ++i)
{
//Define binary predicate functor my_local_predicate

//Test for equality using functor local to loop
std::equal(a.begin(), a.end(), b.begin, my_local_predicate)

 // Do something if they are equal OR unequal

}

If the answer is no, how would one do the above, where the condition for equality keeps changing with every iteration?

NOTE: I tried defining a functor as follows (no for loops here) but the program failed to compile.

#include <algorithm>
#include <iostream>
#include <list>



int main() {

     class EvenOddFunctor 
  {
    int even_;
    int odd_;
    public:
    EvenOddFunctor() : even_(0), odd_(0) {}
    void operator()(int x) {
        if (x%2 == 0) even_ += x;
        else odd_ += x;
    }
    int even_sum() const { return even_; }
    int odd_sum() const { return odd_; }
  };

    EvenOddFunctor evenodd;

    int my_list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    evenodd = std::for_each(my_list,
                  my_list+sizeof(my_list)/sizeof(my_list[0]),
                  evenodd);

    std::cout << "Sum of evens: " << evenodd.even_sum() << "\n";
    std::cout << "Sum of odds: " << evenodd.odd_sum() << std::endl;

    // output:
    // Sum of evens: 30
    // Sum of odds: 25
}

On moving the functor definition before main() the code compiled cleanly, and executed correctly.

So even if defining a functor within a body of a function seems impossible, I would like some nice STL like way of changing the equality condition at every iteration.

  • 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-28T02:31:05+00:00Added an answer on May 28, 2026 at 2:31 am

    For the first part of your question, I don’t see how having a locally defined function is necessary. What you need is just a way to select different functions, which is quite simple. For example:

    std::function<bool(int,int)> my_functions[10];
    my_functions[0] = std::equal_to<int>();
    my_functions[1] = both_divisible_by_5;
    ...etc
    
    for (int i=0; i<10; ++i)
    {
        if (std::equal(a.begin(), a.end(), b.begin, my_functions[i]))
        {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When implementing polymorphic behavior in C++ one can either use a pure virtual method
I know what functors are and when to use them with std algorithms, but
I think they are called functors? (it's been a while) Basically, I want to
I keep hearing a lot about functors in C++. Can someone give me an
Can I get a 'when to use' for these and others? <% %> <%#
Learn You a Haskell has an example about functors. I can read LYAH, and
Can ML functors be practically expressed with .NET interfaces and generics? Is there an
I'm learning about functors this afternoon and am trying to write one for a
Most of the functions in <functional> use functors. If I write a struct like
Generally the standard requires functors to be pure functions because algorithms are allowed to

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.