Re: Elements of constant array as constant expressions.
David R Tribble wrote:
Andrzej 'Foxy' D. wrote:
Could you explain me, why it's impossible to treat an element of
constant array as a constant expression? Look at this code:
const int array[] = { 1 };
template<int I>
struct A {};
void f() {
A<array[0]> a; // !
}
It's obvious that a compiler can know what's the value of array[0],
isn't it? If an array is constant and the index is a constant
expression, then the value of array[0] could be known at
compile-time, treated as a constant expression and used e.g. as a
template argument.
What was the reason not to allow to use array's elements in this way?
Because even though the compiler _could_ know, it is not _required_
to know by the language specification. Technically, the value of an
array element (const or not) is not a compile-time constant. I know
this doesn't sound right to some, but defining array elements as const
does not automatically make them compile-time constants.
The same is true of expressions like "abc"[0].
I believe Andrzej's question wasn't "is it?" but rather "why isn't it?"
We already know that the language specification doesn't require the
elements of const arrays to be recognized as const expressions. The
question is _why the hell not_. I'd like to know as well, BTW.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
---
[ 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 ]