Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: make_pair(T...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 45716 of 48612
Post > Topic >>

Re: make_pair(T1, T2) in <utility>

by "kwikius" <andy@[EMAIL PROTECTED] > May 3, 2008 at 12:20 PM

<subramanian100in@[EMAIL PROTECTED]
> wrote in message 
news:f78d0757-e7b7-4549-a826-62d7ce6f1dae@[EMAIL PROTECTED]
> Given two types T1 and T2, we can create
>
> pair<T1, T2> p(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(value3, 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(value3, 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,T5> const & seq);

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

fun( 
tuple<my::type1,your::t2,std::vector<my::type1>,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
 




 4 Posts in Topic:
make_pair(T1, T2) in
"subramanian100in@[E  2008-05-03 03:57:45 
Re: make_pair(T1, T2) in
gnuyuva <gnuyuva@[EMAI  2008-05-03 04:13:09 
Re: make_pair(T1, T2) in
Ian Collins <ian-news@  2008-05-03 23:16:12 
Re: make_pair(T1, T2) in
"kwikius" <a  2008-05-03 12:20:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 15:38:33 CST 2008.