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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:18:04+00:00 2026-06-07T18:18:04+00:00

I have used std::transform to add some values to existing ones in a list.

  • 0

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;
}
  • 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-07T18:18:06+00:00Added an answer on June 7, 2026 at 6:18 pm

    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.

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

Sidebar

Related Questions

Right now, I have this code: bool isAnyTrue() { for(std::list< boost::shared_ptr<Foo> >::iterator i =
I have a class with a std::map used as a container. I want to
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used some jquery components in my web site, Suddenly i'm getting an
I have used a plugin that uses prototype js, it's working fine but the
I have used this website for testing http://www.webpagetest.org and among some suggestions for optimization
I have used auto complete textbox previously. That auto complete works only when i
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I have this code, int main() { std::string st; std::stringstream ss; ss<<hej hej med
The following code only works when the copy constructor is available. When I add

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.