Topic: Array Length 1
Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Fri, 14 Jul 2006 15:46:06 GMT Raw View
> Imagine a function
>
> template <class T,int n>
> array_tag is_array(T[n]) {}
>
> template <class T>
> not_an_array_tag is_array(T);
Return type?
> Also, arrays are implicitly convertible to pointers, which is not true
> for other objects.
I think it could possibly be designed so that a single object L-value could
be implicitly converted to an array L-value whose length is 1, i.e.:
T obj;
T (&r)[1] = obj;
Functionality wouldn't have to be extended to include "array to pointer
decay", or array indexing.
--
Frederick Gotham
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: "Manfred von Willich" <manfred@techniroot.co.za>
Date: Fri, 14 Jul 2006 10:42:13 CST Raw View
Frederick Gotham wrote:
> ===================================== MODERATOR'S COMMENT:
>
> apparently the orignal posting was lost
>
>
> ===================================== END OF MODERATOR'S COMMENT
> (NOTE to Moderator: I posted this three days ago and it hasn't shown up,
> so I've posted again.)
>
> Should a single object qualify as an array whose length is 1?
>
> #include <iostream>
> #include <cstddef>
>
> template<class T,std::size_t len>
> void Func(T const (&)[len])
> {
> std::cout << "This array contains " << len << " elements.";
> }
>
> int main()
> {
> int array[1];
> int obj;
>
> Func(array);
> Func(obj); /* COMPILE error with g++ */
> }
>
No, no, absolutely no. This would be a monstrous example of fuzziness
creep - solving an inconvenience caused by one fuzzy concept by making
another concept fuzzier to accommodate it. This approach does not
belong in a language such as C++, which already contains far too many
of these problems. Your suggestion would mean that every object is a
de-facto array of infinite dimensions, inter alia: the declaration "int
v;" would allow v[0][0][0][0][0] or equivalently *****v to any depth.
The type checking of the language would be weakened unacceptably.
We should rather be looking at cleaning up a related fuzzy area - the
treatment of an array name as a reference to the array and pointer to
its first element - one of the major flaws in the C/C++ type system.
>
> Given that in C++, a multi-dimensional array is treated as a single-
> dimensional array whose elements are single-dimensional arrays, should we
> be able to pass a multi-dimensional array to the template function shown
> above?
>
>
> int multi[7][6][5];
>
> Func(multi); /* Successful compile with g++ */
>
Yes.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: jpalecek@web.de
Date: Sat, 15 Jul 2006 13:09:05 CST Raw View
Frederick Gotham wrote:
> > Imagine a function
> >
> > template <class T,int n>
> > array_tag is_array(T[n]) {}
> >
> > template <class T>
> > not_an_array_tag is_array(T);
>
>
> Return type?
>
I thought it would be clear
struct array_tag {};
struct not_an_array_tag {};
> > Also, arrays are implicitly convertible to pointers, which is not true
> > for other objects.
>
>
> I think it could possibly be designed so that a single object L-value could
> be implicitly converted to an array L-value whose length is 1, i.e.:
>
> T obj;
>
> T (&r)[1] = obj;
>
>
> Functionality wouldn't have to be extended to include "array to pointer
> decay", or array indexing.
Actually, it would include array indexing with your proposal of
implicit conversion above. And it would do something completely
different for arrays/vectors and for other variables.
Resume: it is a BAD idea to do that implicit conversion, which you
surely
have read from the other post. It is definitely inacceptable to match
variables
to template parameters suggesting arrays, if you imagine that even
implicit conversions do not make a match there, like in
template <class T>
int f (T t1,T t2);
f(1,(unsigned)1); // NOT a match
which is a good feature and allows finer control of the template
parameters.
(You can distinguish two equal types an two implicitly/explicitly
convertible
types).
BTW: if you want your func accept arrays and variables, you can make
an overload or accept T (if you don't wanna distinguish arrays/objects)
etc.
Regards
Jiri Palecek
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Wed, 12 Jul 2006 15:23:26 GMT Raw View
===================================== MODERATOR'S COMMENT:
apparently the orignal posting was lost
===================================== END OF MODERATOR'S COMMENT
(NOTE to Moderator: I posted this three days ago and it hasn't shown up,
so I've posted again.)
Should a single object qualify as an array whose length is 1?
#include <iostream>
#include <cstddef>
template<class T,std::size_t len>
void Func(T const (&)[len])
{
std::cout << "This array contains " << len << " elements.";
}
int main()
{
int array[1];
int obj;
Func(array);
Func(obj); /* COMPILE error with g++ */
}
Given that in C++, a multi-dimensional array is treated as a single-
dimensional array whose elements are single-dimensional arrays, should we
be able to pass a multi-dimensional array to the template function shown
above?
int multi[7][6][5];
Func(multi); /* Successful compile with g++ */
--
Frederick Gotham
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: jpalecek@web.de
Date: Wed, 12 Jul 2006 20:38:43 CST Raw View
> Should a single object qualify as an array whose length is 1?
I think the problem here would be "what IS obj[0]?". If your approach
was accepted, it would mean that for every variable which is not
an array, var[0] would mean var itself, which is not true at present
(if var is some fancy array-like object, like vector, it means
something
completely different).
You can say that the type of obj inthe function would already
resolve this issue, but that's false. Imagine a function
template <class T,int n>
array_tag is_array(T[n]) {}
template <class T>
not_an_array_tag is_array(T);
and code
something f(T t,array_tag) { return t[0]; }
Also, arrays are implicitly convertible to pointers, which is not true
for other objects.
Regards
Jiri Palecek
---
[ 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.comeaucomputing.com/csc/faq.html ]