Addresses of array elements as template non-type paramater
Hi there!
I can't understand why addresses of array elements cannot appear as non-type parameter in class template. I know that something like this:
Code:
int array[3] = {45, 57, 24};
//...
template <int *p>
class X { };
void fun()
{
X<&array[1]> xxx;
}
is dereferencing (in line 8), beacuse:
Code:
&array[1] = &(*(array+1))
But I don't know why compiler can't accept this expression:
Thank you.
Re: Addresses of array elements as template non-type paramater
Which compiler are you using?
Using the online Comeau compiler, this code
Code:
int array[3] = {45, 57, 24};
template <int *p>
class X { };
int main()
{
X<&array[1]> x;
X<array + 1> y;
X<array> z;
}
gives these compiler errors:
Quote:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 8: error: this operator is not allowed in a template argument
expression
X<&array[1]> x;
^
"ComeauTest.c", line 9: error: non-integral operation not allowed in nontype
template argument
X<array + 1> y;
^
"ComeauTest.c", line 8: warning: variable "x" was declared but never referenced
X<&array[1]> x;
^
"ComeauTest.c", line 9: warning: variable "y" was declared but never referenced
X<array + 1> y;
^
"ComeauTest.c", line 10: warning: variable "z" was declared but never referenced
X<array> z;
^
2 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Hit the Back Button to review your code and compile options.
Compiled with C++0x extensions enabled.
Re: Addresses of array elements as template non-type paramater
If the array is global, it *might* be reasonable to do this since the array address could be fixed at compile time. But if it's within any function, then the array address isn't a compile-time constant, and thus can't be part of a template expression.
Re: Addresses of array elements as template non-type paramater
Quote:
Originally Posted by
Lindley
If the array is global, it *might* be reasonable to do this since the array address could be fixed at compile time. But if it's within any function, then the array address isn't a compile-time constant, and thus can't be part of a template expression.
I know... The question is: why I can't get address specific array element... I don't understand Comeau's errors too...
Can you explain this to me ?
Re: Addresses of array elements as template non-type paramater
Quote:
Originally Posted by
Quentin026
Can you explain this to me ?
I think it's saying pointer arithmetics are not allowed in the specification of a template argument.
Re: Addresses of array elements as template non-type paramater
I was trying compile this program:
Code:
template <int *p>
class X { };
int array[3] = {56, 56, 45};
int *p = array+1;
X<p> x;
but Comeau is still displaying this error:
expression must have a constant value
In my opinion there is no difference between array element's address and variable's address...
Re: Addresses of array elements as template non-type paramater
Quote:
Originally Posted by
Quentin026
In my opinion there is no difference between array element's address and variable's address...
That error has nothing to do with pointers.
A template argument must be a compile-time constant. The same error appears here:
Code:
template <int p>
class X { };
int p;
X<p> x;
The correction is this:
Code:
template <int p>
class X { };
int p;
X<10> x; // or something similar
Regards,
Paul McKenzie
Re: Addresses of array elements as template non-type paramater
In line with McKenzie's point, you can manage
const int z1 = 45;
const int z2 = 57;
const int z3 = 24;
....elsewhere
X< z1 > n1;
However,
const int array[ 3 ] = { 45, 57, 24 };
....elsewhere
X< array[1] > n1;
Doesn't.
Re: Addresses of array elements as template non-type paramater
Is this template argument compile-time constant too, although iVar variable is in the other module of program?
Code:
template <int* p>
class X { };
extern iVar; //iVar is type int
X<&iVar> x;
Re: Addresses of array elements as template non-type paramater
Quote:
Originally Posted by
Paul McKenzie
That error has nothing to do with pointers.
A template argument must be a compile-time constant.
Then I don't understand. Apparently, 'array' is a compile time constant, since the declaration of the variable 'z' in main is allowed. This is also what Lindley said above. So why is 'array + 1' not a compile time constant then? I don't see a reason why it couldn't be; array is known, 1 is know, sizeof(int) is known. Hence my conclusion was that pointer arithmetics are not performed at compile time and thus not allowed in the specification of a template argument.
Re: Addresses of array elements as template non-type paramater
That's probably exactly it. Pointer addresses would not be finalized until link time; thus they can't be compile time constant.
Re: Addresses of array elements as template non-type paramater
But pointer's address in this example
Code:
template <int* p>
class X { };
extern iVar; //iVar is type int
X<&iVar> x;
can be compile time constant (although iVar variable is in the other module of program !!!), beacuse I can compile this program without any errors...
Then I don't understand why only array element's address can't be compile time constant.
Re: Addresses of array elements as template non-type paramater
Quote:
Originally Posted by
Quentin026
But pointer's address in this example
There is a difference. The &iVar can only be one value. It can't be changed, therefore it is considered a constant value. Go try and change iVar to point at a different address. You can't do it.
In your previous example, you had *p. This can be changed at any time during the running of the program. That's why it isn't a constant.
Regards,
Paul McKenzie
Re: Addresses of array elements as template non-type paramater
I get it now... Thanks very much :)