Using std:.pair right

D

desktop

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?
 
I

Ian Collins

desktop said:
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?
 
A

Andre Kostur

desktop said:
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?
 
G

Greg Herlihy

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
 
R

Rolf Magnus

desktop said:
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);
 
P

Pete Becker

desktop said:
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)
 
R

Rolf Magnus

Juha said:
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.
 
B

Baltimore

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

Juha Nieminen

Rolf said:
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?
 
J

Juha Nieminen

Erik said:
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.
 
G

Greg Herlihy

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
 
G

Greg Herlihy

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
 
J

James Kanze

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

Jerry Coffin

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);
 
A

Andrew Koenig

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top