Google Groups Home
Help | Sign in
question on construction of pair in <utility>
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
subramanian100in@yahoo.co m, India  
View profile
 More options Aug 20, 12:42 pm
Newsgroups: comp.lang.c++
From: "subramanian10...@yahoo.com, India" <subramanian10...@yahoo.com>
Date: Wed, 20 Aug 2008 04:42:38 -0700 (PDT)
Local: Wed, Aug 20 2008 12:42 pm
Subject: question on construction of pair in <utility>
consider the program:

#include <cstdlib>
#include <iostream>
#include <utility>

using namespace std;

class Test
{
public:
Test(int arg = 0);
Test(const Test &rhs);
Test &operator=(const Test &rhs);

private:
int val;

};

Test::Test(int arg) : val(arg)
{
        cout << "Test one arg ctor" << endl;

}

Test::Test(const Test &rhs) : val(rhs.val)
{
        cout << "Test copy ctor" << endl;

}

Test &Test::operator=(const Test &rhs)
{
        cout << "operator= called" << endl;

        if (this != &rhs)
                val = rhs.val;

        return *this;

}

int main()
{
        Test one(1);

        cout << endl << "pair ctor" << endl;
        pair<int, Test>(1, one);

        cout << endl << "make_pair" << endl;
        make_pair(2, one);

        return EXIT_SUCCESS;

}

I compiled the above program with
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

The output of the above program is:

Test one arg ctor

pair ctor
Test copy ctor

make_pair
Test copy ctor
Test copy ctor

Here make_pair calls one extra Test copy ctor compared to the
pair<int, Test>(1, one).

Will this be the case always ? If so, can it be concluded that
pair<T1, T2>(const T1 &, const T2 &) should be preferred to
make_pair() ?

Kindly clarify.

Thanks
V.Subramanian


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor Bazarov  
View profile
 More options Aug 20, 1:38 pm
Newsgroups: comp.lang.c++
From: Victor Bazarov <v.Abaza...@comAcast.net>
Date: Wed, 20 Aug 2008 08:38:33 -0400
Local: Wed, Aug 20 2008 1:38 pm
Subject: Re: question on construction of pair in <utility>

subramanian10...@yahoo.com, India wrote:
> [.. defining class Test ..]
> Here make_pair calls one extra Test copy ctor compared to the
> pair<int, Test>(1, one).

> Will this be the case always ? If so, can it be concluded that
> pair<T1, T2>(const T1 &, const T2 &) should be preferred to
> make_pair() ?

There is no way to predict when the compiler will decide to forgo
creation of a temporary (that's what you're seeing here).  It is allowed
to construct the object directly if it can, but it is also allowed to
actually use as any number of intermediate objects (temporaries) as the
semantics require.  How 'make_pair' constructs its return value depends
on the implementation and on the way you use that function.

So, in short "no, it will not necessarily always be the case".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google