Topic: Is this legal c++0x
Author: german diago <germandiago@gmail.com>
Date: Fri, 8 May 2009 19:27:37 CST Raw View
If subscript operator is legal as non-member function in c++0x, could
we write?
template <typename...Types, int N>
auto operator[](tuple<Types...> && tup, int n = N) -> constexpr
decltype(get<N>(tup))
{
return get<N>(tup);
}
is this legal c++0x? Because if it is, we could write:
tuple<int, float, string> mytuple;
mytuple[2] = 12.6f;
and expressions in array-like notation.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel.kruegler@googlemail.com
Date: Sat, 9 May 2009 11:21:42 CST Raw View
On 9 Mai, 03:27, german diago <germandi...@gmail.com> wrote:
> If subscript operator is legal as non-member function in c++0x,
Note that the subscript operator still must be written as a non-
static member-function with exactly one parameter as of 13.5.5
[over.sub]/1. The only exception to that would be inside a concept
or concept map, but your code doesn't do that.
> could we write?
>
> template <typename...Types, int N>
> auto operator[](tuple<Types...> && tup, int n = N) -> constexpr
> decltype(get<N>(tup))
> {
> return get<N>(tup);
> }
>
> is this legal c++0x?
Even ignoring the constraints mentioned above this is
ill-formed:
1) The function cannot be a constexpr (and the position of
the specifier is invalid) function, because it uses a reference-
type as one of it's arguments and of the result type (get<>
returns a reference as well). This violates 7.1.5
[dcl.constexpr]/3 b.3:
"each of its parameter types shall be an effective literal type"
2) The meaning of the index parameter n is dubious, because
it's value is completely ignored. This makes the function a
good candidate for an obfuscated code contest, as shown
below. Note that the parameter N cannot be deduced and
because it is the last template argument, you always would
need to provide all template arguments explicitly.
> Because if it is, we could write:
>
> tuple<int, float, string> mytuple;
>
> mytuple[2] = 12.6f;
>
> and expressions in array-like notation.
There are some problems with that snippet beside
those mentioned above:
1) You declared the operator above accepting an rvalue
reference of tuple, but your code attempts to use
an lvalue as argument. This is no longer well-formed as
of the most recent stage of the working paper.
2) Assuming we fix (1) by changing the rvalue-references
by lvalue-references (or adding an overload there-of) what
do you expect will the code do? The argument 2 is
completely ignored and would *not* ensure that N is
deduced as 2.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: google@dalvander.com
Date: Sat, 9 May 2009 11:39:56 CST Raw View
On May 9, 3:27 am, german diago <germandi...@gmail.com> wrote:
> is this legal c++0x? Because if it is, we could write:
>
> tuple<int, float, string> mytuple;
>
> mytuple[2] = 12.6f;
>
> and expressions in array-like notation.
>
Not legal, as the return type would on a argument value, and you
cannot overload functions on argument values.
Regards,
Anders Dalvander
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: litb <Schaub-Johannes@web.de>
Date: Sun, 10 May 2009 15:23:41 CST Raw View
On 9 Mai, 03:27, german diago <germandi...@gmail.com> wrote:
> If subscript operator is legal as non-member function in c++0x, could
> we write?
>
> template <typename...Types, int N>
> auto operator[](tuple<Types...> && tup, int n = N) -> constexpr
> decltype(get<N>(tup))
> {
> return get<N>(tup);
>
> }
>
> is this legal c++0x? Because if it is, we could write:
>
> tuple<int, float, string> mytuple;
>
> mytuple[2] = 12.6f;
>
> and expressions in array-like notation.
>
Another one that could work i think. Yours won't (see daniel's
explanation). First one needs a way to pass a number as both a
function argument and a template argument with as little hassle as
possible. User defined literals!
template<int V, int N> struct pow { static int const value = V *
pow<V, N-1>::value; };
template<int V> struct pow<V, 0> { static int const value = 1; };
template<char... V> struct v2n;
template<char V1, char...V> struct v2n<V1, V...> {
static int const value = (V1-'0') * pow<10,sizeof...(V)>::value +
v2n<V...>::value;
};
template<char V1> struct v2n<V1> { static int const value =
(V1-'0'); };
template<int N> struct i2t { static int const value = N; };
template<char...V> i2t< v2n<V...>::value > operator "" i() { return
{}; }
// in the tuple class template:
template<int N> auto operator[](i2t<N>) -> decltype(get<N>()) { return
get<N>(); }
// some user code...
tuple<int, bool, char> f{1, true, '!'}; assert ( f[1i] == f[0i] );
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]