tmaric
tmaric

Reputation: 5477

Why does std::transform utilize the constructor?

I have used std::transform to add some values to existing ones in a list. The code below works fine, I am just wondering if it is possible to avoid all the calls to the copy constructor (see the output of the program) when executing transform. If I just hack the code, and make a for loop explicitly call the += operator of Base, no copy construction is executed and the values are changed in place which is more efficient.

Can I make transform call the operator+= of Base instead of copy constructing? Should I concentrate on increment<Type>?

The program:

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

template<class T>
class Base;

template<class T>
std::ostream& operator << (std::ostream& os, const Base<T>& b);

template<class T>
class Base
{
    private: 
        T b_;
    public: 
        typedef T value_type;

        Base()
            :
                b_()
        { std::cout << "Base::def ctor" << std::endl; }

        Base (const T& b)
            : 
                b_(b)
        { std::cout << "Base::implicit conversion ctor: " << b_ << std::endl; }

        const T& value()
        {
            return b_;
        }

        const Base operator+(const Base& b) const
        {
            std::cout << "Base operator+ " << std::endl;
            return Base(b_ + b.b_);
        }

        const Base&  operator+=(const T& t) 
        {
            b_ += t;
            return *this;
        }

        friend std::ostream& operator<< <T> (std::ostream& os, const Base<T>& b);
};

template<class T>
std::ostream& operator<< (std::ostream& os, const Base<T>& b)
{
    os << b.b_; 
    return os;
}

template<class Type> 
class increment
{
    typedef typename Type::value_type T; 

    T initial_; 

    public: 

        increment()
            :
                initial_()
        {};

        increment(const T& t)
            :
                initial_(t)
        {}

        T operator()()
        {
            return initial_++;
        }
};

template<class Container>
void write(const Container& c)
{
    std::cout << "WRITE: " << std::endl;
    copy(c.begin(), c.end(), 
         std::ostream_iterator<typename Container::value_type > (std::cout, " "));
    std::cout << std::endl;
    std::cout << "END WRITE" << std::endl;
}

using namespace std;

int main(int argc, const char *argv[])
{
    typedef list<Base<int> > bList; 

    bList baseList(10); 

    cout << "GENERATE" << endl;
    generate_n (baseList.begin(), 10, increment<Base<int> >(10));
    cout << "END GENERATE" << endl;

    write(baseList); 

    // Let's add some integers to Base<int>

    cout << "TRANSFORM: " << endl;

    std::transform(baseList.begin(), baseList.end(), 
                   baseList.begin(), 
                   bind2nd(std::plus<Base<int> >(), 4)); 
    cout << "END TRANSFORM " << endl;

    write(baseList);

    // Hacking the code: 
    cout << "CODE HACKING: " << endl;
    int counter = 4;
    for (bList::iterator it = baseList.begin(); 
         it != baseList.end(); 
         ++it)
    {
        *it += counter; // Force the call of the operator+=
        ++counter;
    }
    write (baseList);
    cout << "END CODE HACKING" << endl;

    return 0;
}

Upvotes: 0

Views: 751

Answers (1)

Dave S
Dave S

Reputation: 21113

Base (const T& b) is not a copy constructor, it's a constructor for Base<T> that accepts a const T&. The copy constructor would normally have the signature Base(const Base& )

That said, your constructor is being invoked every time that you're creating a new Base<int> from an int, which you are doing in your addition operator.

Finally, std::transform() uses the output iterators assignment operator to assign the result of the function to the output. If you want to avoid the copy altogether, you should use std::for_each, along with a std::bind2nd( std::mem_fun_ref(&Base<int>::operator +=), 4 )). This will avoid making a copy, as it will operate soley on the references.

Upvotes: 8

Related Questions