473,811 Members | 3,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

make_pair(T1, T2) in <utility>

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
p = make_pair(value 3, value4);

My doubt is that, when we can do it with pair<T1, T2>(value3, value4)
itself, what is the need for providing make_pair(value 3, value4);

That is, make_pair is considered as an alternative to pair<T1,
T2>(T1_val, T2_val).

Why is make_pair provided in the library ? I am unable to understand
the reason.

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #1
3 1862
On May 3, 3:57 pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Why is make_pair provided in the library ? I am unable to understand
the reason.
Just to make the construct elegant.

consider
struct my_long_struct_ 1 {};
struct my_long_struct_ 2 {};

so its quite difficult (irritating) to write these struct names each
time you make a
new pair. So, make_pair solution is elegant.

template <typename T1, typename T2makepair(T1& v1, T2& v2)
{ return std::pair<T1, T2>(v1, v2); }

It deciphers T1, T2 for you which is so nice. But watchout for stuff
related to
inheritance.

struct A {};
struct B {};
struct C : public A {};

std::pair<A, Bp_ab;
C c1; B b1;
p_ab = std::make_pair( c1, b1); // this is not elegant, and intelligent
though.
Ofcourse, the above statement is not common, but just in case.

Jun 27 '08 #2
su************* *@yahoo.com, India wrote:
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
p = make_pair(value 3, value4);

My doubt is that, when we can do it with pair<T1, T2>(value3, value4)
itself, what is the need for providing make_pair(value 3, value4);
The answer to your question (it is a question, not a doubt) is parameter
matching. The compiler can deduce the types for a function template,
but not for a class template constructor.

--
Ian Collins.
Jun 27 '08 #3

<su************ **@yahoo.comwro te in message
news:f7******** *************** ***********@d19 g2000prm.google groups.com...
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
p = make_pair(value 3, value4);

My doubt is that, when we can do it with pair<T1, T2>(value3, value4)
itself, what is the need for providing make_pair(value 3, value4);

That is, make_pair is considered as an alternative to pair<T1,
T2>(T1_val, T2_val).

Why is make_pair provided in the library ? I am unable to understand
the reason.

Kindly clarify.
This is essentially a workaround for the fact that you cant construct a
templated type without explicitly stating the template parameters.

Say you have a function that takes a tuple ( to extend the issue with pair)
argument

template < typename T1, typename T2, typename T3, typename T4 typename T5>
void fun(tuple<T1,T2 ,T3,T4,T5const & seq);

invoking the function can be tedious if you need to construct a temporary
tuple as you have to each template parameter.

fun(
tuple<my::type1 ,your::t2,std:: vector<my::type 1>,your::t4,her ::t5>(v1,v2,v3, v4,v5)
);

However a template function can deduce its arguments, so by providing
overloaded make_tuple functions the programmers life is made easier and you
don't need to be explicit for the types of v1.. v5 etc, which the compiler
knows already

fun(make_tuple( v1,v2,v3,v4,v5) );

make_pair is exactly the same.

Incidentally it would be interesting to come up with a mechanism where
certain types could be constructed without specifying the parameters, though
using a function does work it means having to write a separate function, so
it would be interesting to see if there is a way to automate it.

regards
Andy Little

Jun 27 '08 #4

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

Similar topics

19
6191
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...
0
1421
by: subramanian100in | last post by:
consider the following program: #include <cstdlib> #include <iostream> #include <map> #include <utility> #include <string> using namespace std;
1
1625
by: subramanian100in | last post by:
consider the program: #include <cstdlib> #include <iostream> #include <utility> using namespace std; class Test {
0
9731
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
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
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
6893
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
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.