When should one explicitly instantiate std::make_pair?

K

K. Frank

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
 
I

Ian Collins

K. Frank said:
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?
 
Ö

Öö Tiib

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::pair<A const,B>',
notice that 'const'. The 'std::pair<A const,B>' and
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::pair<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.
 
M

Marc

K. Frank" said:
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::pair<int,int>, it is shorter than make_pair.
 
G

guy.tristram

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.
 
S

SG

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::pair<int,int>(i,j);

or even

std::pair<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
 
K

K. Frank

That one is easy: never use the explicit form. If you want something
explicit, you can use std::pair<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
 
S

SG

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top