CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2009
    Posts
    28

    Unhappy 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:

    Code:
    X<array + 1> xxx;
    Thank you.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Jul 2009
    Posts
    28

    Unhappy Re: Addresses of array elements as template non-type paramater

    Quote Originally Posted by Lindley View Post
    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 ?

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Addresses of array elements as template non-type paramater

    Quote Originally Posted by Quentin026 View Post
    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

  6. #6
    Join Date
    Jul 2009
    Posts
    28

    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...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Addresses of array elements as template non-type paramater

    Quote Originally Posted by Quentin026 View Post
    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

  8. #8
    Join Date
    Nov 2006
    Posts
    1,611

    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).

  9. #9
    Join Date
    Jul 2009
    Posts
    28

    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;

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Addresses of array elements as template non-type paramater

    Quote Originally Posted by Paul McKenzie View Post
    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

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  12. #12
    Join Date
    Jul 2009
    Posts
    28

    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.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Addresses of array elements as template non-type paramater

    Quote Originally Posted by Quentin026 View Post
    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

  14. #14
    Join Date
    Jul 2009
    Posts
    28

    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
  •  





Click Here to Expand Forum to Full Width

Featured