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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:12:34+00:00 2026-05-31T03:12:34+00:00

I’m using std::make_pair() for this example because pretty much any C++ programmer ought to

  • 0

I’m using std::make_pair() for this example because pretty much any C++ programmer ought to be familiar with it, but more generally I’m wondering about the pattern it uses.

It occurred to me that, although I enjoy the convenience of std::make_pair() it makes an “extra” copy of each argument, since it creates a pair and returns it by value. If I then use that to insert into an STL container that means that actually each parameter is copied a total of 3 times… I wrote this code snippet to illustrate (along with some attempts to improve it without losing too much convenience):

#include <iostream>
#include <utility>
#include <list>

using namespace std;
// C++11 only:
#define MAKE_PAIR(a,b) pair<decltype(a),decltype(b)>((a),(b))

class A {
public:
  A () { }
  A (const A& a) {
    cout << "\tCopy constructor called" << endl;
  }
};

int main()
{
  list<pair<int,A> > l;

  cout << "Using std::make_pair()" << endl;
  l.push_back(make_pair(10,A()));

  cout << "Using MAKE_PAIR()" << endl;
  l.push_back(MAKE_PAIR(10,A()));

  typedef pair<int, A> my_pair;
  cout << "Using a typedef" << endl;
  l.push_back(my_pair(10,A()));
}

which produces the output:

Using std::make_pair()
    Copy constructor called
    Copy constructor called
    Copy constructor called
Using MAKE_PAIR()
    Copy constructor called
    Copy constructor called
Using a typedef
    Copy constructor called
    Copy constructor called

I realize there are a couple other copies here that can probably be eliminated (or rather reduced to pointer/smart pointer copies), for example by using A * or a smart pointer in the pair instead, and allocating it myself.

The macro idea (which requires C++11) seemed interesting to me, although I know many are not fond of macros. The typedef works fine too, but then you have to create a separate typedef for each set of template args, and so it’s more convenient than specifying the template args explicitly each time, it’s still not quite as nice as the wrapper function.

I’m wondering does anybody actually avoid make_pair() in practice for this reason? Does C++/C++11 offer any other interesting solutions?

I like this idea of creating a templated function wrapper around a constructor so that we can deduce the template arguments, but I’m not as crazy about causing a runtime impact because of it.

  • 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-31T03:12:35+00:00Added an answer on May 31, 2026 at 3:12 am

    In my compiler, with and without optimizations, the extra copy constructor was optimized away.

    [10:53pm][wlynch@orange /tmp] c++ --version
    Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
    Target: x86_64-apple-darwin11.3.0
    Thread model: posix
    [10:54pm][wlynch@orange /tmp] c++ -O0 -std=gnu++11 foo.cc -o foo
    [10:54pm][wlynch@orange /tmp] ./foo
    Using std::make_pair()
        Copy constructor called
        Copy constructor called
    Using MAKE_PAIR()
        Copy constructor called
        Copy constructor called
    Using a typedef
        Copy constructor called
        Copy constructor called
    

    If I add the parameter -fno-elide-constructors, then we see the extra constructors.

    [10:57pm][wlynch@orange /tmp] c++ -std=gnu++11 -fno-elide-constructors foo.cc -o foo
    [10:57pm][wlynch@orange /tmp] ./foo
    Using std::make_pair()
        Copy constructor called
        Copy constructor called
        Copy constructor called
        Copy constructor called
    Using MAKE_PAIR()
        Copy constructor called
        Copy constructor called
    Using a typedef
        Copy constructor called
        Copy constructor called
    

    The C++ Spec has this to say on skipping copy constructors in [class.copy.15] of the 2003 Spec:

    When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implemen- tation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.) This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

    • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
    • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.