C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array Length 1

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Frederick Gotham
Guest





PostPosted: Wed Jul 12, 2006 8:23 pm    Post subject: Array Length 1 Reply with quote



===================================== 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 ]
Back to top
Guest






PostPosted: Thu Jul 13, 2006 2:38 am    Post subject: Re: Array Length 1 Reply with quote



Quote:
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 ]
Back to top
Manfred von Willich
Guest





PostPosted: Fri Jul 14, 2006 4:42 pm    Post subject: Re: Array Length 1 Reply with quote



Frederick Gotham wrote:
Quote:
===================================== 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.

Quote:

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 ]
Back to top
Frederick Gotham
Guest





PostPosted: Fri Jul 14, 2006 8:46 pm    Post subject: Re: Array Length 1 Reply with quote

Quote:
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?


Quote:
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 ]
Back to top
Guest






PostPosted: Sat Jul 15, 2006 7:09 pm    Post subject: Re: Array Length 1 Reply with quote

Frederick Gotham wrote:
Quote:
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 {};

Quote:
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 ]
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group