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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:17:10+00:00 2026-06-07T15:17:10+00:00

Suppose I want to create a functor that acts upon some general type. For

  • 0

Suppose I want to create a functor that acts upon some general type.
For example

template<typename Ape>
class functor1
{
   void operator()(Ape& m)
   {
      // Do something to m
   }
};

This has been the standard way of doing things for me. However, I also have another way:

class functor2
{
   template<typename Ape>
   void operator()(Ape& m)
   {
      // Do something to m
   }
};

The advantage of the second approach is that I don’t have to explicitly state the type of the template.

int main()
{
   std::vector<chimpanzee> chimps(100);
   for_each(chimps.begin(), chimps.end(), functor1<chimpanzee>()); // Explicity state the type
   for_each(chimps.begin(), chimps.end(), functor2()); // Less typing. Will it work?
}

Will the second version work? Or am I missing something? If it works are there any advantages to the first approach?

  • 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-07T15:17:12+00:00Added an answer on June 7, 2026 at 3:17 pm

    The obvious difference is that in the first case you specify the type explicitly, and in the second case it is the compiler that will deduce the type for you in the context of the actual call. However, in you specific example it might make no real difference at all.

    1. If your class had multiple member functions in it, the first variant would “fix” the same template parameter for all of them, while in the second variant the parameter would be deduced for each member function independently.

      The same is true when a single member function is called from multiple contexts: each context will perform its own template argument deduction in the second variant.

      It could be good or not so good, depending on your intent.

    2. If your function accepted its argument by value (or by const reference), in the first variant you could specify a different type for the argument than the one stored in the container. For example, you could have created a functor for long and applied it to the container of ints. This is not possible in the second variant.

      For example, if your chimpanzee class was polymorphic, derived from animal, you could have used a functor1<animal> to iterate over such container. In the second variant the template parameter will be deduced for you as chimpanzee, not as animal.

    3. If your class had data members, the second variant would make sure that all specializations of member function(s) share the same data, if the same functor object is used. In the first variant each specialization is a different class, it gets its own data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to create an abstract class in Python with some methods to
Suppose I’m making an Objective-C class that represents a fraction, and want to create
Suppose I want to create a set of observers based on type. That is
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose I want to create a dictionary that maps digits to numbers less than
Suppose I want to create trees of a certain set depth, that is, the
Suppose that i have a city that has many buildings. I want to create
Suppose I want to create, in the spirit of /dev/zero, a file /dev/seven that
Suppose I want to create a control in Java that is like the Symbol
Suppose I want to create a custom FTP server, that is: It will look

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.