Using std:.pair right

Discussion in 'C++' started by desktop, Jun 16, 2007.

  1. desktop

    desktop Guest

    I have made this little test with std::pair:

    test<int> t1(1);
    test<int> t2(2);
    std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);

    where test is a template class I wrote. It seems a bit cumbersome to
    make a pair are there any better way to do it than the above procedure?
     
    desktop, Jun 16, 2007
    #1
    1. Advertising

  2. desktop

    Ian Collins Guest

    desktop wrote:
    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >
    > where test is a template class I wrote. It seems a bit cumbersome to
    > make a pair are there any better way to do it than the above procedure?


    What would you suggest?

    --
    Ian Collins.
     
    Ian Collins, Jun 16, 2007
    #2
    1. Advertising

  3. desktop

    Andre Kostur Guest

    desktop <> wrote in news:f4v7ff$6sd$-c.dk:

    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >
    > where test is a template class I wrote. It seems a bit cumbersome to
    > make a pair are there any better way to do it than the above procedure?
    >


    How would you make it easier? std::pair needs to know the type of each of
    its halves, Your template class needs to know what type it's templated on.
    Where do you see where it could be made easier?
     
    Andre Kostur, Jun 16, 2007
    #3
  4. desktop

    Greg Herlihy Guest

    On 6/15/07 4:30 PM, in article f4v7ff$6sd$-c.dk, "desktop"
    <> wrote:

    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >
    > where test is a template class I wrote. It seems a bit cumbersome to
    > make a pair are there any better way to do it than the above procedure?


    Yes:

    test<int> t1(1);
    test<int> t2(2);

    auto mypair = std::make_pair(t1, t2);

    Unfortunately, you will need a C++ compiler that has implemented this C++09
    feature in order for this code to compile.

    Greg
     
    Greg Herlihy, Jun 16, 2007
    #4
  5. desktop

    benben Guest

    desktop wrote:
    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);


    std::pair<test<int>, test<int> > mypair(t1, t2);

    >
    > where test is a template class I wrote. It seems a bit cumbersome to
    > make a pair are there any better way to do it than the above procedure?
     
    benben, Jun 16, 2007
    #5
  6. desktop

    Rolf Magnus Guest

    desktop wrote:

    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);



    typedef std::pair<test<int>,test<int> > MyPair;

    MyPair mypair(t1, t2);
     
    Rolf Magnus, Jun 16, 2007
    #6
  7. desktop

    Pete Becker Guest

    desktop wrote:
    > I have made this little test with std::pair:
    >
    > test<int> t1(1);
    > test<int> t2(2);
    > std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >
    > where test is a template class I wrote. It seems a bit cumbersome to
    > make a pair are there any better way to do it than the above procedure?


    It depends on what you're trying to accomplish. For your simple example,
    the solution is to not use pair, but write a template class that holds
    two test<T> objects. More generally, just as with iterators, creating
    named objects of pair types often reflects a design mistake. make_pair
    avoids having to write the types involved, which makes it easy to pass
    its return value to a template function:

    template <class T>
    void f(T t);

    f(make_pair(t1, t2));

    Once you start naming objects you've lost the benefit of make_pair,
    because you still have to name the type of the pair. While the new auto
    syntax mentioned in another message can be used to evade this problem,
    before doing that you need to ask why you're creating an auto object in
    the first place.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Jun 16, 2007
    #7
  8. Ian Collins wrote:
    > desktop wrote:
    >> I have made this little test with std::pair:
    >>
    >> test<int> t1(1);
    >> test<int> t2(2);
    >> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >>
    >> where test is a template class I wrote. It seems a bit cumbersome to
    >> make a pair are there any better way to do it than the above procedure?

    >
    > What would you suggest?


    Using 'auto' in the upcoming standard?
     
    Juha Nieminen, Jun 17, 2007
    #8
  9. desktop

    Rolf Magnus Guest

    Juha Nieminen wrote:

    > Ian Collins wrote:
    >> desktop wrote:
    >>> I have made this little test with std::pair:
    >>>
    >>> test<int> t1(1);
    >>> test<int> t2(2);
    >>> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);
    >>>
    >>> where test is a template class I wrote. It seems a bit cumbersome to
    >>> make a pair are there any better way to do it than the above procedure?

    >>
    >> What would you suggest?

    >
    > Using 'auto' in the upcoming standard?


    Best suggestion ever: Wait a few year until they add that feature to the
    language. Then wait another few years until the majority of compilers
    actually implement it. Then you can save a few keypresses.
     
    Rolf Magnus, Jun 17, 2007
    #9
  10. desktop

    Baltimore Guest

    On 17 juin, 11:40, Juha Nieminen <> wrote:
    > Ian Collins wrote:
    > > desktop wrote:
    > >> I have made this little test with std::pair:

    >
    > >> test<int> t1(1);
    > >> test<int> t2(2);
    > >> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);

    >
    > >> where test is a template class I wrote. It seems a bit cumbersome to
    > >> make a pair are there any better way to do it than the above procedure?

    >
    > > What would you suggest?

    >
    > Using 'auto' in the upcoming standard?


    Using a typedef is the easier manner to do this with the current
    standard.

    typedef std::pair<test<int>,test<int> > MyPair;

    The implicit template utilisation is an other beautiful manner,
    without writing the complete type.
     
    Baltimore, Jun 17, 2007
    #10
  11. Rolf Magnus wrote:
    > Best suggestion ever: Wait a few year until they add that feature to the
    > language. Then wait another few years until the majority of compilers
    > actually implement it. Then you can save a few keypresses.


    Any better suggestion for saving the few keypresses?
     
    Juha Nieminen, Jun 17, 2007
    #11
  12. On 2007-06-17 13:20, Juha Nieminen wrote:
    > Rolf Magnus wrote:
    >> Best suggestion ever: Wait a few year until they add that feature to the
    >> language. Then wait another few years until the majority of compilers
    >> actually implement it. Then you can save a few keypresses.

    >
    > Any better suggestion for saving the few keypresses?


    Baltimore's suggestion about using a typedef.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 17, 2007
    #12
  13. desktop

    Rolf Magnus Guest

    Erik Wikström wrote:

    > On 2007-06-17 13:20, Juha Nieminen wrote:
    >> Rolf Magnus wrote:
    >>> Best suggestion ever: Wait a few year until they add that feature to the
    >>> language. Then wait another few years until the majority of compilers
    >>> actually implement it. Then you can save a few keypresses.

    >>
    >> Any better suggestion for saving the few keypresses?

    >
    > Baltimore's suggestion about using a typedef.


    Which - btw- I already made a day before.
     
    Rolf Magnus, Jun 17, 2007
    #13
  14. Erik Wikström wrote:
    >> Any better suggestion for saving the few keypresses?

    >
    > Baltimore's suggestion about using a typedef.


    That only saves keypresses if you have to create more than one
    pair. With only one it requires more keypresses.
     
    Juha Nieminen, Jun 17, 2007
    #14
  15. desktop

    Greg Herlihy Guest

    On Jun 17, 2:44 am, Rolf Magnus <> wrote:
    > Juha Nieminen wrote:
    > > Ian Collins wrote:
    > >> desktop wrote:
    > >>> I have made this little test with std::pair:

    >
    > >>> test<int> t1(1);
    > >>> test<int> t2(2);
    > >>> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);

    >
    > >>> where test is a template class I wrote. It seems a bit cumbersome to
    > >>> make a pair are there any better way to do it than the above procedure?

    >
    > >> What would you suggest?

    >
    > > Using 'auto' in the upcoming standard?

    >
    > Best suggestion ever: Wait a few year until they add that feature to the
    > language. Then wait another few years until the majority of compilers
    > actually implement it. Then you can save a few keypresses.


    Deducing the type of variable from its initializer expression has
    already been voted into C++, so as a feature it has already been
    added. Moreover, I see little reason why the poster would have to wait
    until the "majority" of C++ compilers supported a
     
    Greg Herlihy, Jun 17, 2007
    #15
  16. desktop

    Greg Herlihy Guest

    On Jun 17, 2:44 am, Rolf Magnus <> wrote:
    > Juha Nieminen wrote:
    > > Ian Collins wrote:
    > >> desktop wrote:
    > >>> I have made this little test with std::pair:

    >
    > >>> test<int> t1(1);
    > >>> test<int> t2(2);
    > >>> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);

    >
    > >>> where test is a template class I wrote. It seems a bit cumbersome to
    > >>> make a pair are there any better way to do it than the above procedure?

    >
    > >> What would you suggest?

    >
    > > Using 'auto' in the upcoming standard?

    >
    > Best suggestion ever: Wait a few year until they add that feature to the
    > language. Then wait another few years until the majority of compilers
    > actually implement it. Then you can save a few keypresses.


    Actually, deducing the type of variable from its initializer
    expression has already been voted into C++, so it's only a matter of
    lobbying C++ compiler vendors to support it in their compilers.
    Furthermore, I see no reason why the original poster would have to
    wait until the "majority" of C++ compilers support this feature -
    instead of just the C++ compiler he happens to be using.

    I would point out that the "auto" keyword saves a few keystrokes only
    if you already know what those keystrokes should be. But figuring out
    the return type of a function call may not always be easy (e.g.
    boost::bind) or even be possible (in a portable way). What type, for
    example, does std::tr1::mem_fn() return:

    struct A
    {
    int f();
    };

    ??? mf = std::tr1::mem_fn( &A::f);

    Hint: it's unspecified.

    Greg
     
    Greg Herlihy, Jun 17, 2007
    #16
  17. desktop

    James Kanze Guest

    On Jun 17, 11:44 am, Rolf Magnus <> wrote:
    > Juha Nieminen wrote:
    > > Ian Collins wrote:
    > >> desktop wrote:
    > >>> I have made this little test with std::pair:


    > >>> test<int> t1(1);
    > >>> test<int> t2(2);
    > >>> std::pair<test<int>,test<int> > mypair = std::make_pair(t1,t2);


    > >>> where test is a template class I wrote. It seems a bit cumbersome to
    > >>> make a pair are there any better way to do it than the above procedure?


    > >> What would you suggest?


    > > Using 'auto' in the upcoming standard?


    > Best suggestion ever: Wait a few year until they add that feature to the
    > language. Then wait another few years until the majority of compilers
    > actually implement it. Then you can save a few keypresses.


    More than keystrokes, I think the point is maintainability.
    Modify t1 and t2 to be test<long>, and everything works with
    auto.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 17, 2007
    #17
  18. desktop

    Jerry Coffin Guest

    In article <4675239d$0$1447$>,
    lid says...
    > Erik Wikström wrote:
    > >> Any better suggestion for saving the few keypresses?

    > >
    > > Baltimore's suggestion about using a typedef.

    >
    > That only saves keypresses if you have to create more than one
    > pair. With only one it requires more keypresses.


    I haven't actually counted characters, but I believe this typedef should
    save a few keystrokes even the first time around:

    typedef test<int> ti;

    ti t1(1), t2(2);

    std::pair<ti, ti> mypair=std::make_pair(t1, t2);

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jun 17, 2007
    #18
  19. "desktop" <> wrote in message
    news:f4v7ff$6sd$-c.dk...

    >I have made this little test with std::pair:


    > test<int> t1(1);
    > test<int> t2(2); std::pair<test<int>,test<int> > mypair =
    > std::make_pair(t1,t2);


    > where test is a template class I wrote. It seems a bit cumbersome to make
    > a pair are there any better way to do it than the above procedure?


    How about

    std::pair<test<int>,test<int> > mypair(t1, t2);

    ? It might even be that you can write

    std::pair<test<int>,test<int> > mypair(1, 2);

    depending on your definition of test.
     
    Andrew Koenig, Jun 18, 2007
    #19
    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. Anton
    Replies:
    1
    Views:
    369
    Peter van Merkerk
    Aug 6, 2003
  2. Neil Zanella

    std::pair<,>

    Neil Zanella, Oct 3, 2003, in forum: C++
    Replies:
    14
    Views:
    7,943
    Cy Edmunds
    Oct 6, 2003
  3. Bart Simpson
    Replies:
    6
    Views:
    384
    Diego Martins
    Mar 13, 2006
  4. Jeffrey Walton
    Replies:
    10
    Views:
    943
    Mathias Gaunard
    Nov 26, 2006
  5. Andrey Vul
    Replies:
    2
    Views:
    340
    Andrey Vul
    Nov 26, 2010
Loading...

Share This Page