473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Aug 20 '08 #1
1 1605
su**************@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
Aug 20 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
6
by: pmatos | last post by:
Hi all, Is there a way of (after creating a pair) set its first and second element of not? (pair is definitely a read-only struct)? Cheers, Paulo Matos
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
1
by: arnuld | last post by:
C++ Primer 4/e says, every pair has 2 members named <firstand <lastbut g++ refuses to accept so. I have just created a pir and trying to print its both members: #include<iostream>...
2
by: subramanian100in | last post by:
Suppose left and right are two pair<T1, T2objects. Then 'left < right' returns left.first < right.first || !(right.first < left.first) && left.second < right.second. Suppose left.first is...
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
3
by: subramanian100in | last post by:
Given two types T1 and T2, we can create pair<T1, T2p(value1, value2); where value1 and value2 are of types T1 and T2 respectively. Suppose we want to assign a new pair<value to p. We can do...
9
by: shaun roe | last post by:
Question about pointer-to-data members I have a deeply nested loop, the innermost bit looks a little like this: for(it(vec.begin(), end(vec.end()), it!=end;++it){ if (global == 0){ myObj =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.