//Before:
f(std::get<0>(fib), std::get<1>(fib), std::get<2>(fib), std::get<3>(fib),
std::get<4>(fib));
//Now:
f(fib[c<0>], fib[c<1>], fib[c<2>], fib[c<3>], fib[c<4>]);//Before:
f(std::get<0>(fib), std::get<1>(fib), std::get<2>(fib), std::get<3>(fib),
std::get<4>(fib));
//Now:
f(fib[std::integral_constant<size_t, 0>], fib[std::integral_constant<size_t, <1>], fib[std::integral_constant<size_t, <2>], fib[std::integral_constant<size_t, <3>], fib[std::integral_constant<size_t, <4>]);//Before:
f(get<0>(fib), get<1>(fib), get<2>(fib), get<3>(fib), get<4>(fib));
//Now:
f(fib[c<0>], fib[c<1>], fib[c<2>], fib[c<3>], fib[c<4>]);
What is the advantage here? That it's a member function? Or is it the whole two characters we save in typing `[c<0>]` instead of `get<0>()`?
There's not much motivating this, as far as I can see.
2015-10-18 22:05 GMT+07:00 Nicol Bolas <jmck...@gmail.com>:
What is the advantage here? That it's a member function? Or is it the whole two characters we save in typing `[c<0>]` instead of `get<0>()`?
There's not much motivating this, as far as I can see.One of the guidelines for C++ is to "make it easier to teach and learn". This proposal tries to unify the interface to somethingthat seems more natural to anyone.
Remember that not everyone is as used to C++ as people in this list, and std::get is unnecesarily a different interface for indexing.I see that as a candidate for improving.
The problem I see is, that the behaviours is utterly inconsistent with other standard types.
So you expect the operator[] to always return the same type.
Now if you want to have it consistent, you should be able to do basicly everything with a tuple, which you could also do with a container.
The problem I see is, that the behaviours is utterly inconsistent with other standard types.So you expect the operator[] to always return the same type.II tend to look at this as indexing an ordered sequence. Makes sense. Why should the type be the same?
Now if you want to have it consistent, you should be able to do basicly everything with a tuple, which you could also do with a container.I do not pretend a tuple is a container, but, still, indexing is a minimum expectation.
I think the first thing a newcomer to C++would expect from a tuple is to be able to index them.
for(int x : irange(0, tpl.count()))
{
auto &value = tpl[x];
}Are iterators even possible?
On Sunday, October 18, 2015 at 8:23:09 PM UTC-4, Germán Diago wrote:The problem I see is, that the behaviours is utterly inconsistent with other standard types.So you expect the operator[] to always return the same type.II tend to look at this as indexing an ordered sequence. Makes sense. Why should the type be the same?
Because pretty much every other index-able class returns the same type from its operator[].
Now if you want to have it consistent, you should be able to do basicly everything with a tuple, which you could also do with a container.I do not pretend a tuple is a container, but, still, indexing is a minimum expectation.
A minimum expectation from whom?
If you look around, indexing is typically something you do to containers (usually contiguous) or views thereof. So your argument seems self-refuting: if users aren't supposed to see a tuple as a container, then they aren't going to expect to be able to index it.
I think the first thing a newcomer to C++would expect from a tuple is to be able to index them.
A newcomer from some other language with native tuples? Possibly. But from other near-C++ languages (like Java or C#, neither of which have [] indexing), or simply neophyte programmers? Their first question will be... what's a tuple?
And from the description, I doubt they'll expect to be able to index it.
However, let's say that "a newcomer to C++" would indeed expect a tuple to be indexable. Would they not also expect it to be indexable with an integer, not some funny integral_constant thing? A newcomer who didn't understand much about the language would be non-plussed to learn that this simple loop isn't possible:for(int x : irange(0, tpl.count()))
{
auto &value = tpl[x];
}
They can't even do `tpl[c<x>()]`, which to a newbie makes perfect sense. The compiler will give them some funny error they don't understand.
If we're going to start speculating about what newcomers would expect, I think it's just as reasonable to think that, if you give them indexing, they'll expect integer indexing. At least with `std::get`, the interface is so different that there is less of a surprise when `get<x>` errors out.
Are iterators even possible?
Look at Boost.Fusion.
Now, those won't be STL iterators. But they serve the purpose of an iterator, and they do work in (meta)algorithms.
Remember that not everyone is as used to C++ as people in this list, and std::get is unnecesarily a different interface for indexing.I see that as a candidate for improving.
some_function_template<index>(tup)
i.e.
get<index>(tup)
auto foo()
{
struct
{
int value;
double x;
} result = {1, 0.5};
return result;
}
auto foo()
{
return NEW((value, 1)(x, 0.5));
}
El lunes, 19 de octubre de 2015, 8:02:58 (UTC+7), Nicol Bolas escribió:On Sunday, October 18, 2015 at 8:23:09 PM UTC-4, Germán Diago wrote:
It is natural to map that to index, and not to a "special-case" provided std::get. But this is open to opinion actually. I just see that it is one less rule/function to remember:want to access nth element of something? use operator[]. Want to access key of an associative container? Use operator[] (most of the time, I know the limitations).
They can't even do `tpl[c<x>()]`, which to a newbie makes perfect sense. The compiler will give them some funny error they don't understand.
If we're going to start speculating about what newcomers would expect, I think it's just as reasonable to think that, if you give them indexing, they'll expect integer indexing. At least with `std::get`, the interface is so different that there is less of a surprise when `get<x>` errors out.Well, go to 20 people that have used C++ only for some weeks. Ask them: how would you access the nth element of a tuple? How many do you think would say operator[],
and how many std::get (which maybe they do not even know).
My bet is on operator[].
And it is still integer access, just that needs compile-time deduction, hence,std::integer_constant is what we need.
On Sunday, October 18, 2015 at 9:46:07 PM UTC-4, Germán Diago wrote:El lunes, 19 de octubre de 2015, 8:02:58 (UTC+7), Nicol Bolas escribió:On Sunday, October 18, 2015 at 8:23:09 PM UTC-4, Germán Diago wrote:
It is natural to map that to index, and not to a "special-case" provided std::get. But this is open to opinion actually. I just see that it is one less rule/function to remember:want to access nth element of something? use operator[]. Want to access key of an associative container? Use operator[] (most of the time, I know the limitations).
std::set and its variations don't overload operator[]. Neither does std::list.
So it would seem that `operator[]` is not nearly as ubiquitous as you claim. If a user wants the second value in a `list`, they can't use `operator[]` for that.
They can't even do `tpl[c<x>()]`, which to a newbie makes perfect sense. The compiler will give them some funny error they don't understand.
If we're going to start speculating about what newcomers would expect, I think it's just as reasonable to think that, if you give them indexing, they'll expect integer indexing. At least with `std::get`, the interface is so different that there is less of a surprise when `get<x>` errors out.
Well, go to 20 people that have used C++ only for some weeks. Ask them: how would you access the nth element of a tuple? How many do you think would say operator[],and how many std::get (which maybe they do not even know).
Neither. They'll ask what a tuple is.
My bet is on operator[].
You'd be wrong, because none of them even know what `operator[]` is. They might say that you use `[]` notation, but they certainly would know nothing of operator overloading.
So they're not going to say "use operator[]". They're going to say `tuple[4]`. Which you cannot in your proposal. They would be no more correct under your proposal than they would be under the current paradigm.
If C++ gets the ability to use regular old integer variables with tuple access, I'd be more willing to see operator[] work on it. But unless `operator[]` is going to behave like regular `operator[]` overloads (ie: operating on seemingly runtime values), then I don't see the point. If retrieving elements of a tuple has to be done using different syntax from other types, I'd rather that the syntax be very different.
And it is still integer access, just that needs compile-time deduction, hence,std::integer_constant is what we need.
And you think that's a concept which C++ programmers who've been writing code for "a few weeks" ought to be introduced to?
As an alternative to what you say: Is std::get<N>(t) template access newcomer-friendly to explain? No.What is easier to teach?1. People know operator[] and they are taught about tuples. For tuples you teach them: tuple[1is]. (Rule is: put number, add is)OR2. You teach them about function std::get<N>, which has a totally different interface, and start to tell them about passing a number in between that strange <> sign.
+1 for op[] with variant returns in the order of the tuple. That's cool.
Can a variant store references?
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
El lunes, 19 de octubre de 2015, 10:39:44 (UTC+7), Nicol Bolas escribió:On Sunday, October 18, 2015 at 9:46:07 PM UTC-4, Germán Diago wrote:El lunes, 19 de octubre de 2015, 8:02:58 (UTC+7), Nicol Bolas escribió:On Sunday, October 18, 2015 at 8:23:09 PM UTC-4, Germán Diago wrote:
It is natural to map that to index, and not to a "special-case" provided std::get. But this is open to opinion actually. I just see that it is one less rule/function to remember:want to access nth element of something? use operator[]. Want to access key of an associative container? Use operator[] (most of the time, I know the limitations).
std::set and its variations don't overload operator[]. Neither does std::list.I start to find these arguments too weak. std::list has O(n) access for that. std::set is not associative.
They can't even do `tpl[c<x>()]`, which to a newbie makes perfect sense. The compiler will give them some funny error they don't understand.
If we're going to start speculating about what newcomers would expect, I think it's just as reasonable to think that, if you give them indexing, they'll expect integer indexing. At least with `std::get`, the interface is so different that there is less of a surprise when `get<x>` errors out.You have to compare alternatives: operator[] vs std::get<N>. Now your requirement is a *plain* integer. Just cause you said so, right?
So they're not going to say "use operator[]". They're going to say `tuple[4]`. Which you cannot in your proposal. They would be no more correct under your proposal than they would be under the current paradigm.tuple[4] vs tuple[4is] --- is there really a huge difference?
Is std::get<> better? Why?
And it is still integer access, just that needs compile-time deduction, hence,std::integer_constant is what we need.
And you think that's a concept which C++ programmers who've been writing code for "a few weeks" ought to be introduced to?As an alternative to what you say: Is std::get<N>(t) template access newcomer-friendly to explain? No.What is easier to teach?1. People know operator[] and they are taught about tuples. For tuples you teach them: tuple[1is]. (Rule is: put number, add is)
OR2. You teach them about function std::get<N>, which has a totally different interface, and start to tell them about passing a number in between that strange <> sign.
+1 for op[] with variant returns in the order of the tuple. That's cool.
Can a variant store references?
+1 for op[] with variant returns in the order of the tuple. That's cool.
Can a variant store references?
auto operator[]( constexpr int idx ) -> decltype(std::tuple_element<idx,T>::type){
return std::get<idx>(*this);
}The problem I see is, that the behaviours is utterly inconsistent with other standard types.So you expect the operator[] to always return the same type.II tend to look at this as indexing an ordered sequence. Makes sense. Why should the type be the same?
Because pretty much every other index-able class returns the same type from its operator[].
A minimum expectation from whom?
...
I think the first thing a newcomer to C++would expect from a tuple is to be able to index them.
A newcomer from some other language with native tuples? Possibly. But from other near-C++ languages (like Java or C#, neither of which have [] indexing), or simply neophyte programmers? Their first question will be... what's a tuple?
And from the description, I doubt they'll expect to be able to index it.
They can't even do `tpl[c<x>()]`, which to a newbie makes perfect sense. The compiler will give them some funny error they don't understand.
If we're going to start speculating about what newcomers would expect, I think it's just as reasonable to think that, if you give them indexing, they'll expect integer indexing. At least with `std::get`, the interface is so different that there is less of a surprise when `get<x>` errors out.
If C++ gets the ability to use regular old integer variables with tuple access, I'd be more willing to see operator[] work on it. But unless `operator[]` is going to behave like regular `operator[]` overloads (ie: operating on seemingly runtime values), then I don't see the point. If retrieving elements of a tuple has to be done using different syntax from other types, I'd rather that the syntax be very different.
Cannot be done, but that proposal is pretty close to that ideal and is more uniform than having std::get<N>.