473,799 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=(cons t Test &rhs);

private:
int val;
};

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

Test::Test(cons t 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 1624
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
45872
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 on what you are using the pair for. For instance if I am using coordinates in two dimensional space then I like to use x and y. So I might as well define my own struct with x and y members in it and create a constructor so
19
6167
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
6
2755
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
7285
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
1840
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> #include<string> #include<utility>
2
1391
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 NOT less than right.first.
18
2511
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 container. I have written both these operators as non-friend functions.
3
1862
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 it as p = pair<T1, T2>(value3, value4); where value3 and value4 are of types T1 and T2 respectively. The last statement can also be written as
9
3725
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 = it->func().second->anObj(); } else { myObj = it->func().first->anObj(); }
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.