Topic: Lvalue pair


Author: "Samee Zahur" <samee.zahur@gmail.com>
Date: 19 Sep 2005 17:10:25 GMT
Raw View
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"<<endl;
else cout<<"Data inserted successfully"<<endl;
//... 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 <class A,class B> class lvalue_pair
{
  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                       ]





Author: David Abrahams <dave@boost-consulting.com>
Date: 19 Sep 2005 18:20:02 GMT
Raw View
"Samee Zahur" <samee.zahur@gmail.com> writes:

> 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"<<endl;
> else cout<<"Data inserted successfully"<<endl;
> //... 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                       ]





Author: ben-public-nospam@decadentplace.org.uk (Ben Hutchings)
Date: Mon, 19 Sep 2005 18:18:15 GMT
Raw View
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                       ]





Author: "Samee Zahur" <samee.zahur@gmail.com>
Date: 20 Sep 2005 03:30:01 GMT
Raw View
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                       ]





Author: caj@cs.york.ac.uk (chris jefferson)
Date: Wed, 21 Sep 2005 04:17:49 GMT
Raw View
Samee Zahur wrote:
> 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                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 21 Sep 2005 04:17:52 GMT
Raw View
"Samee Zahur" <samee.zahur@gmail.com> writes:

> 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                       ]