Re: Array irregularites in C++
Al wrote:
A literal is static (constant).
I don't agree.
Then we are disagreeing on terminology. :-)
Using my terminology (see my reply to Seungbeom Kim) a literal exists
in the compile-time realm, and its size and contents are known. That
doesn't prevent any dynamic structure to be initialized by a literal,
of course.
This idea that a literal is static/constant in fact is
irregular by your own definition.
That may be so, but there's a limit to regularity as well. If the
language ignores some fundamental property, such as the "compile-time"
vs "run-time" determinacy discussed here, the language sacrifices
expressivity for uniformity.
If the language were /truly/ regular
in this regard, the following would be possible:
{1, 2, 3}.pop();
In my view, the reason why this should not be allowed is because you
try to mutate a constant. This is a regular rule.
Without the ability to express a static (compile-time) property the
language becomes less expressive. You can clearly deduce that the
written expression "{1, 2, 3}" contains three elements of int. If the
language uses the "{}" braces to denote arrays, then you can go on to
deduce that the expression is an "array <int, 3>"; a static constant.
If the latter is not expressible you lose something (generic
programming and meta-programming is the processing of static entities;
types and constants).
That is not to say that something like what you wrote shouldn't be
possible. But conceptually it expresses a conversion of a static
construct to a dynamic one. I prefer to make that clear:
// Initialize a stack with an array literal.
// typeid ({1, 2, 3}) == typeid (array <int, 3>)
stack <int> ({1, 2, 3}).pop ();
// The above requires a stack constructor:
template <class T, size_t N>
stack <T>::stack (array <T, N>&);
This conveniently allows you to initialize many types of dynamic
structures by literals, which of course is something C++ already
depends on.
While distinguishing between static and dynamic constructs implies
differences, it doesn't have to imply irregularity or lack of safety,
and both type of constructs can be designed to work very similarly both
conceptually and syntactically. For example:
array <int, 3> a; // Static constant size.
const vector <int> b (3); // Constant size.
a = array <int, 3> (); // Error: const violation.
b = vector <int> (); // Error: const violation.
a.push_back (1); // Error: syntax error.
b.push_back (1); // Error: const violation.
int i = a [5]; // Error: out-of-bounds.
int j = b [5]; // Error: out-of-bounds.
The latter two may not be detected in C++ at compile-time, but
theoretically nothing prevents it. It is possible to deduce the sizes
of both the array and the vector. So a static type is very similar to a
constant dynamic type. In the extreme you could define array as a
synonym for a const vector:
template <class T, size_t N>
alias array = const vector <T, N>;
In short, there need not be much irregularity between static and
dynamic types.
Of course, it could implicitly *convert* to a dynamic array though.
How would this happen implicitly? You mean a copy is made behind the
scenes? What would the syntax look like?
C++ does this all the time:
float f = 1; // implicitly converted
void print (std::string s);
print ("Hello"); // implicitly converted
In C++0x, the following will (hopefully) be possible:
void print (std::vector <int> v);
print ({1, 2, 3}); // implicitly converted
As long as native, unsafe arrays exist with /much/ more convenient
syntax, then people will use them.
See my reply to Seungbeom Kim for some really nice and regular syntax.
However, I don't understand what you mean by vector being
"built on the static array." [...]
Well, both are dealing with memory --- a static array.
Well, I see your point, but if we're going with semantics then
everything is a static array. The whole memory space. In fact, the whole
program itself too, I guess. But how is that useful?
It's obviously very useful; you just described the von Neumann
architecture in terms of a basic language construct. That's expressive!
:-)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]