views:

49

answers:

4

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?

A: 

You are only printing "Constructor" from the default constructor. Instances of your type can come into existance from the copy-constructor as well. Copies might be being made through the call stack. Unless you specify otherwise you get a default copy constructor for free. You can add a copy constructor and also print out "Constructor" if you want to verify this.

No matter how it's constructed, you will still see the one-and-only destructor fire and print-out "Destructor".

Doug T.
+4  A: 

Don't forget about the copy constructor (the Rule of Three can help you to remember this):

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

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

    print(const print& other) {
        std::cout << "Copy 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;
}

Output:

After assigning values 
Constructor 
Copy Constructor 
10
20
30
Copy Constructor 
Destructor
Destructor
Destructor
After printing values
joshperry
When I add the copy constructor, the output is as follows,Constructor102030Copy ConstructorDestructorDestructor.This beats me even more.
Different compilers will have different copy semantics... My code was compiled with GCC with no optimizations (on codepad: http://codepad.org/11kRuFXO) so the number of copies isn't significant, as long as you have the same number of destructor calls as you have objects that were created.
joshperry
Thanks Mr.Josh. Appreciate your explanations.
A: 

I would say that the for_each created copies of the print() functor which calls the implicit copy constructor defined by the compiler which doesn't appear in your code (not the same as your defined constructor above).

David
+1  A: 

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

Chubsdad
Excellent. Now, I got why I got the copy constructor working at the end of the for loop. Thanks a ton.