By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Using std:.pair right

P: n/a
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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?
Jun 15 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
desktop wrote:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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.
Jun 15 '07 #2

P: n/a
desktop <ff*@sss.comwrote in news:f4**********@news.net.uni-c.dk:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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?
Jun 15 '07 #3

P: n/a

On 6/15/07 4:30 PM, in article f4**********@news.net.uni-c.dk, "desktop"
<ff*@sss.comwrote:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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<intt1(1);
test<intt2(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

Jun 16 '07 #4

P: n/a
desktop wrote:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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?
Jun 16 '07 #5

P: n/a
desktop wrote:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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);

Jun 16 '07 #6

P: n/a
desktop wrote:
I have made this little test with std::pair:

test<intt1(1);
test<intt2(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<Tobjects. 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)
Jun 16 '07 #7

P: n/a
Ian Collins wrote:
desktop wrote:
>I have made this little test with std::pair:

test<intt1(1);
test<intt2(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?
Jun 17 '07 #8

P: n/a
On 17 juin, 11:40, Juha Nieminen <nos...@thanks.invalidwrote:
Ian Collins wrote:
desktop wrote:
I have made this little test with std::pair:
test<intt1(1);
test<intt2(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.

Jun 17 '07 #9

P: n/a
Juha Nieminen wrote:
Ian Collins wrote:
>desktop wrote:
>>I have made this little test with std::pair:

test<intt1(1);
test<intt2(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.

Jun 17 '07 #10

P: n/a
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?
Jun 17 '07 #11

P: n/a
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
Jun 17 '07 #12

P: n/a
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.

Jun 17 '07 #13

P: n/a
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.
Jun 17 '07 #14

P: n/a
On Jun 17, 2:44 am, Rolf Magnus <ramag...@t-online.dewrote:
Juha Nieminen wrote:
Ian Collins wrote:
desktop wrote:
I have made this little test with std::pair:
> test<intt1(1);
test<intt2(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

Jun 17 '07 #15

P: n/a
On Jun 17, 2:44 am, Rolf Magnus <ramag...@t-online.dewrote:
Juha Nieminen wrote:
Ian Collins wrote:
desktop wrote:
I have made this little test with std::pair:
> test<intt1(1);
test<intt2(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
Jun 17 '07 #16

P: n/a
On Jun 17, 11:44 am, Rolf Magnus <ramag...@t-online.dewrote:
Juha Nieminen wrote:
Ian Collins wrote:
desktop wrote:
I have made this little test with std::pair:
> test<intt1(1);
test<intt2(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: ja*********@gmail.com
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
Jun 17 '07 #17

P: n/a
In article <46**********************@news.song.fi>,
no****@thanks.invalid 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<intti;

ti t1(1), t2(2);

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

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 17 '07 #18

P: n/a
"desktop" <ff*@sss.comwrote in message
news:f4**********@news.net.uni-c.dk...
>I have made this little test with std::pair:
test<intt1(1);
test<intt2(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.
Jun 18 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.