J
Juha Nieminen
Assume something like this:
namespace
{
const int array[] = { 1, 2, 3 };
struct A
{
static const int value = array[0];
};
}
This gives the error "an array reference cannot appear in a
constant-expression".
My question: Why not? What would be the technical problem? You can
certainly use const ints in constant-expressions, so why can't elements
of a const array be used?
This is a hindrance. I had a situation where I had such a const array
of integral values, and it would have been useful to get the sum of
those values at compile time. Template metaprogramming could be used to
iterate over the array and sum the values... except that the elements of
an array cannot be used in compile-time constant expressions. Rather
annoying.
namespace
{
const int array[] = { 1, 2, 3 };
struct A
{
static const int value = array[0];
};
}
This gives the error "an array reference cannot appear in a
constant-expression".
My question: Why not? What would be the technical problem? You can
certainly use const ints in constant-expressions, so why can't elements
of a const array be used?
This is a hindrance. I had a situation where I had such a const array
of integral values, and it would have been useful to get the sum of
those values at compile time. Template metaprogramming could be used to
iterate over the array and sum the values... except that the elements of
an array cannot be used in compile-time constant expressions. Rather
annoying.