-
July 15th, 2009, 09:13 AM
#1
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.
-
July 15th, 2009, 09:43 AM
#2
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:
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 15th, 2009, 10:30 AM
#3
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.
-
July 15th, 2009, 11:08 AM
#4
Re: Addresses of array elements as template non-type paramater
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 ?
-
July 15th, 2009, 11:58 AM
#5
Re: Addresses of array elements as template non-type paramater
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 15th, 2009, 01:22 PM
#6
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...
-
July 15th, 2009, 02:05 PM
#7
Re: Addresses of array elements as template non-type paramater
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
-
July 15th, 2009, 02:16 PM
#8
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.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
July 15th, 2009, 02:17 PM
#9
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;
-
July 15th, 2009, 02:20 PM
#10
Re: Addresses of array elements as template non-type paramater
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 15th, 2009, 02:35 PM
#11
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.
-
July 15th, 2009, 03:24 PM
#12
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.
-
July 15th, 2009, 03:32 PM
#13
Re: Addresses of array elements as template non-type paramater
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
-
July 15th, 2009, 04:11 PM
#14
Re: Addresses of array elements as template non-type paramater
I get it now... Thanks very much
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|