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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:00:46+00:00 2026-05-16T10:00:46+00:00

The following example, beats me. I’ve been so far thinking, that when functor is

  • 0

The following example, beats me. I’ve been so far thinking, that when functor is being used, the object gets constructed once and the same object is used multiple times, when used with for_each algorithm and that seems to be correct.

However, even though, only one object gets constructed, but multiple objects are destroyed. Now, this beats me.

class print
{
public:
    void operator()(int i)
    {
        std::cout << i << std::endl;
    }

    print()
    {
        std::cout << "Constructor " << std::endl;
    }
    ~print()
    {
        std::cout << "Destructor" << std::endl;
    }
};

int main()
{

    std::vector<int> v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);

    std::cout << "After assigning values " << std::endl;
    for_each(v.begin() , v.end() , print());
    std::cout << "After printing values " << std::endl;
}

The output is as follows

After assigning Values
Constructor
10
20
30
Destructor
Destructor
Destructor
After printing values.

How is this possible?

  • 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-16T10:00:47+00:00Added an answer on May 16, 2026 at 10:00 am

    Here is how I have for_each on my system:

    template<class _InIt,
        class _Fn1> inline
        _Fn1 _For_each(_InIt _First, _InIt _Last, _Fn1 _Func)  
        {   // perform function for each element
        for (; _First != _Last; ++_First)
            _Func(*_First);
        return (_Func);                 // a copy could be created here (C3)
        }
    
        template<class _InIt,
            class _Fn1> inline
            _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
            {   // perform function for each element
            _DEBUG_RANGE(_First, _Last);
            _DEBUG_POINTER(_Func);
            return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func)); // a copy created here (C2)
            }
    

    So, essentially, this is how it could look

    for_each(v.begin() , v.end() , print());       // print functor created (constructor)
                                                   // a copy created here (C1)
    

    Now, this is completely upto the implementation if copy elision is done or not. The Standard does allow that latitude to elide away the copy constructor e.g. C3 may be eliminated.

    A good way to control gcc behavior here is the -fno-elide-constructors which ensures that code does not elide away the copy constructor

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

Sidebar

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.