Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > When should one explicitly instantiate std::make_pair?

Reply
Thread Tools

When should one explicitly instantiate std::make_pair?

 
 
K. Frank
Guest
Posts: n/a
 
      05-28-2013
Hello Group!

As a simple example, consider:

int i = 3;
int j = 4;
auto p1 = std::make_pair (i, j); // no explicit types
auto p2 = std::make_pair<int, int> (i, j); // explicit

The reason I ask is because some time ago, I tried
explicitly instantiating make_pair (in a more complicated
setting, the details of which I don't recall, but I
think it involved iterators for a template collection
class), and I couldn't get it to work -- I got all kinds
of confusing template compiler error messages. Then I
"wised up" and just called make_pair without giving it
explicit types, and everything worked fine.

But now I see some sample code (on www.cplusplus.com)
where make_pair is used with explicit types. So what's
the right way to use make_pair, and what's the right
way to think about what's going on?

Thanks.


K. Frank
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-28-2013
K. Frank wrote:
> Hello Group!
>
> As a simple example, consider:
>
> int i = 3;
> int j = 4;
> auto p1 = std::make_pair (i, j); // no explicit types
> auto p2 = std::make_pair<int, int> (i, j); // explicit
>
> The reason I ask is because some time ago, I tried
> explicitly instantiating make_pair (in a more complicated
> setting, the details of which I don't recall, but I
> think it involved iterators for a template collection
> class), and I couldn't get it to work -- I got all kinds
> of confusing template compiler error messages. Then I
> "wised up" and just called make_pair without giving it
> explicit types, and everything worked fine.
>
> But now I see some sample code (on www.cplusplus.com)
> where make_pair is used with explicit types. So what's
> the right way to use make_pair, and what's the right
> way to think about what's going on?
>


make_pair is a function template, so the compiler should be able to
deduce the types in most situations.

Was the use case where you had errors inside another template?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      05-28-2013
On Tuesday, 28 May 2013 08:08:21 UTC+3, K. Frank wrote:
> As a simple example, consider:
>
> int i = 3;
> int j = 4;
> auto p1 = std::make_pair (i, j); // no explicit types
> auto p2 = std::make_pair<int, int> (i, j); // explicit


I commonly use first form if I need it.

> The reason I ask is because some time ago, I tried
> explicitly instantiating make_pair (in a more complicated
> setting, the details of which I don't recall, but I
> think it involved iterators for a template collection
> class), and I couldn't get it to work -- I got all kinds
> of confusing template compiler error messages.


There are few places in library where novices often stumble.
For example 'std::map<A,B>::value_type' is 'std:air<A const,B>',
notice that 'const'. The 'std:air<A const,B>' and
'std:air<A,B>' are different types and that sometimes causes
confusion to novices.

> So what's
> the right way to use make_pair, and what's the right
> way to think about what's going on?


Other alternative is to typedef the pair that you use:

typedef std:air<int,std::string> IdName;

Then just construct one:

functionTakingIdName( IdName(x.id(),x.name()) );

There are nothing odd going on, pair is very simple. It is meant
for cases when you need just a pair of values and making special
class for that feels overkill.
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      05-28-2013
"K. Frank" wrote:

> Hello Group!
>
> As a simple example, consider:
>
> int i = 3;
> int j = 4;
> auto p1 = std::make_pair (i, j); // no explicit types
> auto p2 = std::make_pair<int, int> (i, j); // explicit


That one is easy: never use the explicit form. If you want something
explicit, you can use std:air<int,int>, it is shorter than make_pair.
 
Reply With Quote
 
guy.tristram@gmail.com
Guest
Posts: n/a
 
      05-28-2013
On Tuesday, May 28, 2013 6:08:21 AM UTC+1, K. Frank wrote:
> The reason I ask is because some time ago, I tried
> explicitly instantiating make_pair (in a more complicated


How long ago is "some time ago"? If I recall correctly, an earlier
version of MSVC didn't like explicit instanciations of function
templates, although that probably wasn't the case here because
(again IIRC) it didn't reject the syntax as long as it could also
deduce the template parameters from the function arguments.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-28-2013
On May 28, 7:08*am, K. Frank wrote:
>
> As a simple example, consider:
>
> * *int i = 3;
> * *int j = 4;
> * *auto p1 = std::make_pair (i, j); *// no explicit types
> * *auto p2 = std::make_pair<int, int> (i, j); *// explicit


In almost all cases of function templates you are discouraged to
specify the template parameters explicitly. In case of the standard
library function templates the only exception that comes to my mind
right now is std::forward where deduction is turned off on purpose by
using a "nondeducible context" for the parameter type.

The point of make_pair is in fact not having to specify the template
arguments but to have them deduced by the compiler. If you want to be
explicit about it, there is no need to use make_pair at all:

auto p2 = std:air<int,int>(i,j);

or even

std:air<int,int> p2 (i,j);

Also, your line

*auto p2 = std::make_pair<int, int> (i, j);

does not compile anymore since C++11 because make_pair turned into a
perfect forwarding function template. It's now defined to be

template<class T, class U>
pair<X,Y> make_pair(T && x, U && y);

with some nontrivial type transformation T->X and U->Y. If you specify
T and U to be int, the function parameters x and y will be rvalue
references of type int&& which you cannot initialize with lvalues of
type int. This would be a compile-time error.

> But now I see some sample code (on www.cplusplus.com)
> where make_pair is used with explicit types.


I don't see these examples. At least they are not here
http://www.cplusplus.com/reference/utility/make_pair/

> So what's
> the right way to use make_pair, and what's the right
> way to think about what's going on?


I think I answered that already.

> Thanks.
>
> K. Frank


Cheers!
SG
 
Reply With Quote
 
K. Frank
Guest
Posts: n/a
 
      05-30-2013
On Tuesday, May 28, 2013 3:19:10 AM UTC-4, Marc wrote:
> "K. Frank" wrote:
>
> > Hello Group!
> >
> > As a simple example, consider:
> >
> > int i = 3;
> > int j = 4;
> > auto p1 = std::make_pair (i, j); // no explicit types
> > auto p2 = std::make_pair<int, int> (i, j); // explicit

>
> That one is easy: never use the explicit form. If you want something
> explicit, you can use std:air<int,int>, it is shorter than make_pair.


Thanks for everyone's comments. That helps clear things up.

I will stick with using make_pair without explicit types, and
deem the code fragment I saw on www.cplusplus.com that did use
explicit types to be a minor bit of noise.

A quick off-topic question:

As you can see, I am posting through google groups. When I
reply to a post, google groups inserts a bunch of blank
lines in the quoted text. Very annoying. Is there any way
to turn that off?

Thanks.


K. Frank
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-30-2013
On May 30, 4:32*am, "K. Frank" wrote:
>
> A quick off-topic question:
>
> As you can see, I am posting through google groups. *When I
> reply to a post, google groups inserts a bunch of blank
> lines in the quoted text. *Very annoying. *Is there any way
> to turn that off?


Complain to Google about it and switch to the old Google Groups
interface for now.

Alternativly: Ditch Google Groups and get yourself a proper
newsreader.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting Error :: explicitly instantiate functional Template Pallav singh C++ 3 09-15-2008 10:07 AM
Should one use std::string*, std::map* to avoid copying ? digz C++ 3 01-09-2008 01:39 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
query on std::make_pair() mark.van.dijk@perform-sol.com C++ 3 03-07-2005 09:01 PM
pair and make_pair JustSomeGuy C++ 3 07-07-2004 09:30 AM



Advertisments