Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using std:.pair right

Reply
Thread Tools

Using std:.pair right

 
 
desktop
Guest
Posts: n/a
 
      06-15-2007
I have made this little test with std:air:

test<int> t1(1);
test<int> t2(2);
std:air<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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-15-2007
desktop wrote:
> I have made this little test with std:air:
>
> test<int> t1(1);
> test<int> t2(2);
> std:air<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.
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      06-15-2007
desktop <(E-Mail Removed)> wrote in news:f4v7ff$6sd$(E-Mail Removed)-c.dk:

> I have made this little test with std:air:
>
> test<int> t1(1);
> test<int> t2(2);
> std:air<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:air 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?
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      06-15-2007



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

> I have made this little test with std:air:
>
> test<int> t1(1);
> test<int> t2(2);
> std:air<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

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      06-16-2007
desktop wrote:
> I have made this little test with std:air:
>
> test<int> t1(1);
> test<int> t2(2);
> std:air<test<int>,test<int> > mypair = std::make_pair(t1,t2);


std:air<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?

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-16-2007
desktop wrote:

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



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

MyPair mypair(t1, t2);

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      06-16-2007
desktop wrote:
> I have made this little test with std:air:
>
> test<int> t1(1);
> test<int> t2(2);
> std:air<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)
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-17-2007
Ian Collins wrote:
> desktop wrote:
>> I have made this little test with std:air:
>>
>> test<int> t1(1);
>> test<int> t2(2);
>> std:air<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?
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-17-2007
Juha Nieminen wrote:

> Ian Collins wrote:
>> desktop wrote:
>>> I have made this little test with std:air:
>>>
>>> test<int> t1(1);
>>> test<int> t2(2);
>>> std:air<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.

 
Reply With Quote
 
Baltimore
Guest
Posts: n/a
 
      06-17-2007
On 17 juin, 11:40, Juha Nieminen <(E-Mail Removed)> wrote:
> Ian Collins wrote:
> > desktop wrote:
> >> I have made this little test with std:air:

>
> >> test<int> t1(1);
> >> test<int> t2(2);
> >> std:air<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:air<test<int>,test<int> > MyPair;

The implicit template utilisation is an other beautiful manner,
without writing the complete type.

 
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
Dynamic Menu Items is not right aligned with Right to Left documen =?Utf-8?B?QmlzaG95?= ASP .Net 0 12-28-2006 11:39 AM
Right-Click With Mouse and Toolba Buttonsr Don't Work Right Bigfoot It Is Computer Support 0 10-30-2006 06:08 PM
Tool to enable Right click on pages where Right click is disabled tsteinke@gmail.com Computer Support 4 08-28-2005 11:53 PM
Tool to right click image in windows explorer and rotate image right or left 90 degrees siliconpi Digital Photography 2 11-29-2004 12:56 PM
pass the right form input to the right control Tom ASP .Net 0 12-11-2003 03:07 AM



Advertisments