<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


|