Allowing std::tie

121 views
Skip to first unread message

Joshua Bell

unread,
Nov 11, 2015, 6:44:29 PM11/11/15
to c...@chromium.org
std::tie (from <tuple>) allows unpacking std::tuple members. While we don't use std::tuple yet (we have base::Tuple; perhaps that should be a prerequisite?), there's a useful idiom for multi-member comparisons:

bool Foo::operator<(const Foo& rhs) const {
  return std:tie(a_, b_, c_) < std::tie(rhs.a_, rhs.b_, rhs.c_); 
}

Sample CL: https://codereview.chromium.org/1438073002/

Nico Weber

unread,
Nov 12, 2015, 1:09:32 PM11/12/15
to Joshua Bell, c...@chromium.org
Since std::tie uses tuples, allowing it in general is probably blocked on coming up with a plan for base::Tuple vs std::tuple – filed http://crbug.com/554987 for that. (Also, using it for `a, b = c, d` style assignments looks kind of weird to me.)

Allowing it just for implementing operator< sounds like a fine plan in the meantime to me, since getting operator< wrong is surprisingly common.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAD649j5b0rzTATUOhGb3dWLYqmbxEoX0_r0%3DndV7ZaRruh5E0g%40mail.gmail.com.

Dana Jansens

unread,
Nov 13, 2015, 5:13:11 PM11/13/15
to Nico Weber, Joshua Bell, cxx
On Thu, Nov 12, 2015 at 10:09 AM, Nico Weber <tha...@chromium.org> wrote:
Since std::tie uses tuples, allowing it in general is probably blocked on coming up with a plan for base::Tuple vs std::tuple – filed http://crbug.com/554987 for that. (Also, using it for `a, b = c, d` style assignments looks kind of weird to me.)

(a, b = c, d looks like a weird way to get around "one statement per line" more-or-less, but maybe makes more sense with functions returning tuples.)
 
Allowing it just for implementing operator< sounds like a fine plan in the meantime to me, since getting operator< wrong is surprisingly common.

+1
 

On Wed, Nov 11, 2015 at 3:44 PM, Joshua Bell <jsb...@chromium.org> wrote:
std::tie (from <tuple>) allows unpacking std::tuple members. While we don't use std::tuple yet (we have base::Tuple; perhaps that should be a prerequisite?), there's a useful idiom for multi-member comparisons:

bool Foo::operator<(const Foo& rhs) const {
  return std:tie(a_, b_, c_) < std::tie(rhs.a_, rhs.b_, rhs.c_); 
}

Sample CL: https://codereview.chromium.org/1438073002/

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAD649j5b0rzTATUOhGb3dWLYqmbxEoX0_r0%3DndV7ZaRruh5E0g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.

Vladimir Levin

unread,
Nov 13, 2015, 5:21:32 PM11/13/15
to Dana Jansens, Nico Weber, Joshua Bell, cxx
On Fri, Nov 13, 2015 at 2:12 PM, 'Dana Jansens' via cxx <c...@chromium.org> wrote:
On Thu, Nov 12, 2015 at 10:09 AM, Nico Weber <tha...@chromium.org> wrote:
Since std::tie uses tuples, allowing it in general is probably blocked on coming up with a plan for base::Tuple vs std::tuple – filed http://crbug.com/554987 for that. (Also, using it for `a, b = c, d` style assignments looks kind of weird to me.)

(a, b = c, d looks like a weird way to get around "one statement per line" more-or-less, but maybe makes more sense with functions returning tuples.)

To be fair, since tuples are also implicitly constructable from pairs, you can have nice uses of tie like:

std::map<Foo, Bar>::iterator it;
bool is_new_value;
std::tie(it, is_new_value) = map.insert(std::make_pair(foo, bar));

or even

bool new_value_added;
std::tie(std::ignore, new_value_added) = map.insert(std::make_pair(foo, bar));

although the latter example is the same as map.insert(std::make_pair(foo, bar)).second

 
Allowing it just for implementing operator< sounds like a fine plan in the meantime to me, since getting operator< wrong is surprisingly common.

+1
 

On Wed, Nov 11, 2015 at 3:44 PM, Joshua Bell <jsb...@chromium.org> wrote:
std::tie (from <tuple>) allows unpacking std::tuple members. While we don't use std::tuple yet (we have base::Tuple; perhaps that should be a prerequisite?), there's a useful idiom for multi-member comparisons:

bool Foo::operator<(const Foo& rhs) const {
  return std:tie(a_, b_, c_) < std::tie(rhs.a_, rhs.b_, rhs.c_); 
}

Sample CL: https://codereview.chromium.org/1438073002/

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAD649j5b0rzTATUOhGb3dWLYqmbxEoX0_r0%3DndV7ZaRruh5E0g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiF81jx0ViXJRHtnRhvYk1n%3DgF2zyu3-WC1VHY-5fcKUDw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
Reply all
Reply to author
Forward
0 new messages