C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Lvalue pair

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Samee Zahur
Guest





PostPosted: Mon Sep 19, 2005 5:10 pm    Post subject: Lvalue pair Reply with quote



I've really not been following recent developments in the upcoming
standard (or even boost lib for that matter) , so I'd like to
know if anything like this is under consideration:

For any function returning a heterogenous container like pair (or maybe
even tuple if a part of the new standard), I'd like to receive the
values into a bunch of 'loose' variables rather than having to declare
a pair object of an ugly type. For example, map::insert returns a pair,
so I'd like to be able to code this:

map<int,int> mymap;
map<int,int>::iterator it; //In real code iterator would be typedefed


//.. use it for some other task maybe, or not.

bool success;
lvpair(it,success)=mymap.insert(make_pair(4,1));

if(!success) cout<<"A value already exists for key 4"< else cout<<"Data inserted successfully"< //... some other code


This allows me to avoid extra declarations and avoid use of
generic identifier names more easily ( if(!success)...
rather than if(!res.second)... )

So I was thinking if a light-weight addition was possible (if
not already proposed).
--------------------------------------------------------------
template {
A& aref;
B& bref;
public:
lvalue_pair(A& ainit,B& binit) : aref(ainit), bref(binit) {}
template <class Asrc, class Bsrc>
lvalue_pair& operator=(std::pair<Asrc,Bsrc> p)
{aref=p.first; bref=p.second;return *this;}
template <class Asrc, class Bsrc>
lvalue_pair& operator=(const lvalue_pair& p)
{aref=p.aref; bref=p.bref; return *this;}
};

template <class A,class B> lvalue_pair<A,B> lvpair(A& ainit,B& binit)
{return lvalue_pair<A,B>(ainit,binit);}
---------------------------------------------------------------
Seemed like a pretty useful addition to me (could be extended
to tuples), although it needs objects to be assignable.

Samee

PS. seriously, does the stl map::value_type have to be a plain
pair object? I'd like something at least derived from it instead,
so that I have codes like it->key,it->value rather than it->first,
it->second. That way it could simply have key and value aliased/
referenced to first and second. Old codes don't complain, new ones
look better :)

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 19, 2005 6:18 pm    Post subject: Re: Lvalue pair Reply with quote



I think what you're looking for is std::tr1::tie (aka boost::tie).

Ben.

--
Ben Hutchings
Never attribute to conspiracy what can adequately be explained by stupidity.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
David Abrahams
Guest





PostPosted: Mon Sep 19, 2005 6:20 pm    Post subject: Re: Lvalue pair Reply with quote



"Samee Zahur" <samee.zahur (AT) gmail (DOT) com> writes:

Quote:
I'd like to be able to code this:

map<int,int> mymap;
map<int,int>::iterator it; //In real code iterator would be typedefed


//.. use it for some other task maybe, or not.

bool success;
lvpair(it,success)=mymap.insert(make_pair(4,1));

if(!success) cout<<"A value already exists for key 4"< else cout<<"Data inserted successfully"< //... some other code

That functionality is spelled "tie" and it is available here:
http://www.boost.org/libs/tuple/doc/tuple_users_guide.html#tiers

Yes, it's also in the TR1 tuples proposal.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Samee Zahur
Guest





PostPosted: Tue Sep 20, 2005 3:30 am    Post subject: Re: Lvalue pair Reply with quote

Thanks, really saved my day.
It was getting somewhat common to write this little thing up wherever I
use these utilities. Good to know that I won't have to very soon. Say,
how soon are we likely to see these things implemented?

Samee

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
chris jefferson
Guest





PostPosted: Wed Sep 21, 2005 4:17 am    Post subject: Re: Lvalue pair Reply with quote

Samee Zahur wrote:
Quote:
Thanks, really saved my day.
It was getting somewhat common to write this little thing up wherever I
use these utilities. Good to know that I won't have to very soon. Say,
how soon are we likely to see these things implemented?


If you don't mind fiddling around for a while to get it installed (or at
least I have trouble, maybe I'm just stupid), the excellent boost
(www.boost.org) already contains implementations of most (perhaps all?)
of the elements of tr1, and in particular definatly has tuple (a
generalisation of pair) and tie. Things in boost may in theory differ
slightly from TR1, but I believe in the case of tuple and tie they are
the same.

I can't speak for any other compilers, but I know that g++ started
supporting a number of TR1 components in version 4.0, and support is
improving at every release. Tuple and tie have both been fully supported
since 4.0. As a tiny example (which doesn't actually do anything..)

#include <tr1/tuple>
#include <tr1/utility> // NOTE: this also adds <utility>
using namespace std;
using namespace tr1;

int main(void)
{
tuple<int, int, int> t(1,2,3);
int a,b,c;
tie(a,b,c) = t;
}

sets a=1, b=2, c=3, as you might expect :)

Chris

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
David Abrahams
Guest





PostPosted: Wed Sep 21, 2005 4:17 am    Post subject: Re: Lvalue pair Reply with quote

"Samee Zahur" <samee.zahur (AT) gmail (DOT) com> writes:

Quote:
Thanks, really saved my day.
It was getting somewhat common to write this little thing up wherever I
use these utilities. Good to know that I won't have to very soon. Say,
how soon are we likely to see these things implemented?

Huh? You can download Boost from http://www.boost.org today. That
feature has been in boost for years.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.