R
Rade
Please have a look at the following program:
#include <iostream>
template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value = array[index];
};
extern const int array[] = { 1, 2, 3, 4, 5 };
int main()
{
std::cout <<
ArrayIndex<array, 0>::value << "," <<
ArrayIndex<array, 1>::value << "," <<
ArrayIndex<array, 2>::value << "," <<
ArrayIndex<array, 3>::value << "," <<
ArrayIndex<array, 4>::value;
}
Output is, on my C++ compiler (VS 2003): 0,0,0,0,0 while I'd expect that the
output is 1,2,3,4,5. What actually does the C++ Standard say about this? In
other words, is the above program correct and if it is, what should be its
output with a standard-compliant compiler ? Moreover, if the program is
correct, is the expression 'array[index]' within the ArrayIndex class
regarded as a constant expression ?
(note that I had to declare the array with 'extern', which shouldn't be
necessary by the Standard, as this array has external linkage, anyway.
However, the compiler issues an error if I don't do this. So I have reasons
to suspect that the compiler is not standard-compliant).
Regards,
Rade
#include <iostream>
template <const int array[], size_t index>
class ArrayIndex
{
public:
static const int value = array[index];
};
extern const int array[] = { 1, 2, 3, 4, 5 };
int main()
{
std::cout <<
ArrayIndex<array, 0>::value << "," <<
ArrayIndex<array, 1>::value << "," <<
ArrayIndex<array, 2>::value << "," <<
ArrayIndex<array, 3>::value << "," <<
ArrayIndex<array, 4>::value;
}
Output is, on my C++ compiler (VS 2003): 0,0,0,0,0 while I'd expect that the
output is 1,2,3,4,5. What actually does the C++ Standard say about this? In
other words, is the above program correct and if it is, what should be its
output with a standard-compliant compiler ? Moreover, if the program is
correct, is the expression 'array[index]' within the ArrayIndex class
regarded as a constant expression ?
(note that I had to declare the array with 'extern', which shouldn't be
necessary by the Standard, as this array has external linkage, anyway.
However, the compiler issues an error if I don't do this. So I have reasons
to suspect that the compiler is not standard-compliant).
Regards,
Rade