473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2058
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.comwro te 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**********@ne ws.net.uni-c.dk, "desktop"
<ff*@sss.comwro te:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
45872
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 on what you are using the pair for. For instance if I am using coordinates in two dimensional space then I like to use x and y. So I might as well define my own struct with x and y members in it and create a constructor so
7
2063
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 seen.insert(new, new); i need to do it that way but the code brakes when i write the command "seen.insert(new, new);"
3
1253
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 after them. Tnaks in advance for any help. jd //file test.cpp: #include "pair.h" #include "triple.h"
2
3408
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, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
5
5719
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. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
13
9681
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
7799
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 field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit = accumulate( numberOfFreqSlotsToAdd.begin(),
0
2065
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> (sc_logic vector with 22 bits) and the value is a class pf packets with 8 different fields of type sc_lv also. Here's what I am trying to do - cur_transactions_p current_pkt_info;
10
1588
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 the latter for it's more descriptive. However, in the latter implementation (equivalent of which is shown below), I got some compiler errors on gcc 3.3.1 with MyClass. #include<map> #include <string>
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.