473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using std:.pair right

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
18 2018
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
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

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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
7
by: Stoian | last post by:
hey how is it goin, i've got this problem - declared a template but it doesn't work properly, here is the code: 53 std::map<Config, Config> seen; 54 Config new(); 55 ...
3
by: jd | last post by:
The following code illustrates a problem I have been trying to get around in c++. Can anyone explain how I can get this to work. The code compiles fine if I leave out the lines with the comments...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: cesco | last post by:
I have a set of pairs defined as follow: set< pair<UserEquipment*, double> > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second...
0
by: Mona | last post by:
Newbie question - I am a newbie to c++ and stl maps. My question is based onSystemC which is a specialized C++ hardware programming language. I am trying to create a map with key of type sc_lv<22>...
10
by: puzzlecracker | last post by:
I need to be able to map two string to some value in the map. I consider two alternatives to implement : using pair class or using a separate class/struct containing two member strings. I prefer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.