When should one explicitly instantiate std::make_pair?

Discussion in 'C++' started by K. Frank, May 28, 2013.

  1. K. Frank

    K. Frank Guest

    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
    K. Frank, May 28, 2013
    #1
    1. Advertising

  2. K. Frank

    Ian Collins Guest

    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
    Ian Collins, May 28, 2013
    #2
    1. Advertising

  3. K. Frank

    Öö Tiib Guest

    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::pair<A const,B>',
    notice that 'const'. The 'std::pair<A const,B>' and
    'std::pair<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::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.
    Öö Tiib, May 28, 2013
    #3
  4. K. Frank

    Marc Guest

    "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::pair<int,int>, it is shorter than make_pair.
    Marc, May 28, 2013
    #4
  5. K. Frank

    Guest

    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.
    , May 28, 2013
    #5
  6. K. Frank

    SG Guest

    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::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
    SG, May 28, 2013
    #6
  7. K. Frank

    K. Frank Guest

    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::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
    K. Frank, May 30, 2013
    #7
  8. K. Frank

    SG Guest

    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.
    SG, May 30, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    pair and make_pair

    JustSomeGuy, Jul 7, 2004, in forum: C++
    Replies:
    3
    Views:
    19,029
    Darkay Li
    Jul 7, 2004
  2. John Black

    can not reference make_pair?

    John Black, Nov 17, 2004, in forum: C++
    Replies:
    2
    Views:
    586
    ES Kim
    Nov 17, 2004
  3. Replies:
    3
    Views:
    5,821
    Pete Becker
    Mar 7, 2005
  4. digz
    Replies:
    3
    Views:
    374
    Howard
    Jan 9, 2008
  5. Pallav singh
    Replies:
    3
    Views:
    268
    Ian Collins
    Sep 15, 2008
Loading...

Share This Page