templated pointer function for simple array

Discussion in 'C++' started by shaun, Jan 27, 2006.

  1. shaun

    shaun Guest

    Dear all,
    I realized an error in a previous post, I reproduce it here because I'm
    still not sure how to solve it:

    I want to make a templated function which points to one-past-the-end of
    a simple array, to pass to a range constructor for a const vector.
    Here is some demonstration code:

    #include <iostream>
    using namespace std;
    const double a[]={
    2.3,4.5,6.6,8.0,10.0
    };

    template<class T> T * endof( T parray[]){
    int elementSize = sizeof(parray[0]);
    int arraySize = sizeof(parray);
    cout<<"Inside the function:"<<endl;
    cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
    cout<<"Pointer to array start "<<parray<<endl;
    int numel= arraySize/elementSize;
    return (parray+numel);
    };

    int main (int argc, char * const argv[]) {
    const double * pEnd=endof(a);
    cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    cout<<"Outside the function: "<<endl;
    cout<<"Array size: "<<sizeof(a);
    cout<<" Element size: "<<sizeof(a[0])<<endl;
    cout<<"Pointer to array start: "<<a<<endl;
    int numElements=sizeof(a)/sizeof(a[0]);
    const double * pEnd2= a+numElements;
    cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    return 0;
    }

    ...but my templated function doesn't work at all, here is the output:

    Inside the function:
    Arraysize: 4 Element size: 8
    Pointer to array start 0xe9ef0
    Returned pointer: 0xe9ef0

    Outside the function:
    Array size: 40 Element size: 8
    Pointer to array start: 0xe9ef0
    Pointer to one-past-end: 0xe9f18

    Pointer to a[5]0xe9f18

    .....
    I can see that inside the function, the argument parray[] has somehow
    lost its 'arrayness', so the sizeof(parray) returns the size of the
    pointer. Outside the function, this works fine...am i missing some
    detail of syntax or is my whole philosophy crooked?

    cheers

    shaun
     
    shaun, Jan 27, 2006
    #1
    1. Advertising

  2. shaun

    Rolf Magnus Guest

    shaun wrote:

    > Dear all,
    > I realized an error in a previous post, I reproduce it here because I'm
    > still not sure how to solve it:
    >
    > I want to make a templated function which points to one-past-the-end of
    > a simple array, to pass to a range constructor for a const vector.
    > Here is some demonstration code:
    >
    > #include <iostream>
    > using namespace std;
    > const double a[]={
    > 2.3,4.5,6.6,8.0,10.0
    > };
    >
    > template<class T> T * endof( T parray[]){


    This function takes a pointer to T.

    > int elementSize = sizeof(parray[0]);
    > int arraySize = sizeof(parray);


    This will give you the size of a pointer to T.

    > cout<<"Inside the function:"<<endl;
    > cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
    > cout<<"Pointer to array start "<<parray<<endl;
    > int numel= arraySize/elementSize;
    > return (parray+numel);
    > };
    >
    > int main (int argc, char * const argv[]) {
    > const double * pEnd=endof(a);
    > cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    > cout<<"Outside the function: "<<endl;
    > cout<<"Array size: "<<sizeof(a);
    > cout<<" Element size: "<<sizeof(a[0])<<endl;
    > cout<<"Pointer to array start: "<<a<<endl;
    > int numElements=sizeof(a)/sizeof(a[0]);
    > const double * pEnd2= a+numElements;
    > cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    > cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    > return 0;
    > }
    >
    > ..but my templated function doesn't work at all, here is the output:
    >
    > Inside the function:
    > Arraysize: 4 Element size: 8
    > Pointer to array start 0xe9ef0
    > Returned pointer: 0xe9ef0
    >
    > Outside the function:
    > Array size: 40 Element size: 8
    > Pointer to array start: 0xe9ef0
    > Pointer to one-past-end: 0xe9f18
    >
    > Pointer to a[5]0xe9f18
    >
    > ....
    > I can see that inside the function, the argument parray[] has somehow
    > lost its 'arrayness', so the sizeof(parray) returns the size of the
    > pointer.


    Yes. It decayed into a pointer.

    > Outside the function, this works fine...am i missing some detail of syntax
    > or is my whole philosophy crooked?


    You can't pass arrays by value to a function. They will always get converted
    to pointers. You'd probably not want to pass the array anway, because that
    would copy the whole thing (passing by value means that the passed object
    gets copied into the argument of the funciton)
    What you can do is pass a reference:

    template<class T, int Size> T * endof( T (&parray)[Size]){
    return parray + Size;
    }
     
    Rolf Magnus, Jan 27, 2006
    #2
    1. Advertising

  3. shaun

    Ben Radford Guest

    shaun wrote:
    > Dear all,
    > I realized an error in a previous post, I reproduce it here because I'm
    > still not sure how to solve it:
    >
    > I want to make a templated function which points to one-past-the-end of
    > a simple array, to pass to a range constructor for a const vector.
    > Here is some demonstration code:
    >
    > #include <iostream>
    > using namespace std;
    > const double a[]={
    > 2.3,4.5,6.6,8.0,10.0
    > };
    >
    > template<class T> T * endof( T parray[]){
    > int elementSize = sizeof(parray[0]);
    > int arraySize = sizeof(parray);
    > cout<<"Inside the function:"<<endl;
    > cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
    > cout<<"Pointer to array start "<<parray<<endl;
    > int numel= arraySize/elementSize;
    > return (parray+numel);
    > };
    >
    > int main (int argc, char * const argv[]) {
    > const double * pEnd=endof(a);
    > cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    > cout<<"Outside the function: "<<endl;
    > cout<<"Array size: "<<sizeof(a);
    > cout<<" Element size: "<<sizeof(a[0])<<endl;
    > cout<<"Pointer to array start: "<<a<<endl;
    > int numElements=sizeof(a)/sizeof(a[0]);
    > const double * pEnd2= a+numElements;
    > cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    > cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    > return 0;
    > }
    >
    > ..but my templated function doesn't work at all, here is the output:
    >
    > Inside the function:
    > Arraysize: 4 Element size: 8
    > Pointer to array start 0xe9ef0
    > Returned pointer: 0xe9ef0
    >
    > Outside the function:
    > Array size: 40 Element size: 8
    > Pointer to array start: 0xe9ef0
    > Pointer to one-past-end: 0xe9f18
    >
    > Pointer to a[5]0xe9f18
    >
    > ....
    > I can see that inside the function, the argument parray[] has somehow
    > lost its 'arrayness', so the sizeof(parray) returns the size of the
    > pointer. Outside the function, this works fine...am i missing some
    > detail of syntax or is my whole philosophy crooked?
    >
    > cheers
    >
    > shaun


    Since you didn't specify a size for parray the compiler has no idea how
    big it is. So you can't expect sizeof(parray) to give the size of the
    array. Infact 'T parray[]' is just syntactic sugar for 'T* parray', so
    you are correct in that it is just taking the size of a pointer.
    Something like the following might work:

    #include <iostream>
    using namespace std;
    double a[5] = {
    2.3,4.5,6.6,8.0,10.0
    };

    template<class T, int N> T endof(T arr[N]) {
    return (arr + N);
    };

    int main (int argc, char * const argv[]) {
    double* pEnd = endof<double, 5>(a);

    cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    cout<<"Outside the function: "<<endl;
    cout<<"Array size: "<<sizeof(a);
    cout<<" Element size: "<<sizeof(a[0])<<endl;
    cout<<"Pointer to array start: "<<a<<endl;
    int numElements=sizeof(a)/sizeof(a[0]);
    const double * pEnd2= a+numElements;
    cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    return 0;
    }

    But so far I can only get it to work with explicit instansiation of the
    endof template and that kind of defeats the point of using it in the
    first place. Maybe someone with a better knowledge of the ins and outs
    of templates can help?
     
    Ben Radford, Jan 27, 2006
    #3
  4. shaun

    Kai-Uwe Bux Guest

    shaun wrote:

    > Dear all,
    > I realized an error in a previous post, I reproduce it here because I'm
    > still not sure how to solve it:
    >
    > I want to make a templated function which points to one-past-the-end of
    > a simple array, to pass to a range constructor for a const vector.
    > Here is some demonstration code:
    >
    > #include <iostream>
    > using namespace std;
    > const double a[]={
    > 2.3,4.5,6.6,8.0,10.0
    > };
    >
    > template<class T> T * endof( T parray[]){
    > int elementSize = sizeof(parray[0]);
    > int arraySize = sizeof(parray);
    > cout<<"Inside the function:"<<endl;
    > cout<<"Arraysize: "<<arraySize<<" Element size: "<<elementSize<<endl;
    > cout<<"Pointer to array start "<<parray<<endl;
    > int numel= arraySize/elementSize;
    > return (parray+numel);
    > };
    >
    > int main (int argc, char * const argv[]) {
    > const double * pEnd=endof(a);
    > cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    > cout<<"Outside the function: "<<endl;
    > cout<<"Array size: "<<sizeof(a);
    > cout<<" Element size: "<<sizeof(a[0])<<endl;
    > cout<<"Pointer to array start: "<<a<<endl;
    > int numElements=sizeof(a)/sizeof(a[0]);
    > const double * pEnd2= a+numElements;
    > cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    > cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    > return 0;
    > }
    >
    > ..but my templated function doesn't work at all, here is the output:
    >


    Hm, ponder a bit about:


    template < typename T, unsigned long N >
    unsigned long length_of ( T const (& arg) [N] ) {
    return( N );
    }

    #include <iostream>

    const double a[] = { 2.3, 4.5, 6.6, 8.0, 10.0 };

    int main ( void ) {
    std::cout << length_of( a ) << '\n';
    }


    I am not sure if the standard guarantees that this code prints "5" because I
    am a little shaky about the precise meaning of the line

    const double a[] = { 2.3, 4.5, 6.6, 8.0, 10.0 };


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 27, 2006
    #4
  5. shaun

    shaun Guest

    OK, thanks all...after some experimenting, I came up with:

    #include <iostream>
    using namespace std;
    const double a[]={
    2.3,4.5,6.6,8.0,10.0
    };

    template<class T> T * endof( T parray[], long unsigned int s){
    return (parray+s/sizeof(parray[0]));
    };

    int main (int argc, char * const argv[]) {
    const double * pEnd=endof(a,sizeof(a));
    cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    cout<<"Outside the function: "<<endl;
    cout<<"Array size: "<<sizeof(a);
    cout<<" Element size: "<<sizeof(a[0])<<endl;
    cout<<"Pointer to array start: "<<a<<endl;
    int numElements=sizeof(a)/sizeof(a[0]);
    const double * pEnd2= a+numElements;
    cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    return 0;
    }


    .....

    This still allows me to add extra 'magic numbers' in my const array at
    the top of the file without changing any subsequent code. I'm not
    entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
    feels like the 'sizeof' should be redundant, but I understand why it
    isn't.


    cheers

    shaun
     
    shaun, Jan 27, 2006
    #5
  6. shaun

    Rolf Magnus Guest

    shaun wrote:

    > OK, thanks all...after some experimenting, I came up with:
    >
    > #include <iostream>
    > using namespace std;
    > const double a[]={
    > 2.3,4.5,6.6,8.0,10.0
    > };
    >
    > template<class T> T * endof( T parray[], long unsigned int s){
    > return (parray+s/sizeof(parray[0]));
    > };
    >
    > int main (int argc, char * const argv[]) {
    > const double * pEnd=endof(a,sizeof(a));
    > cout<<"Returned pointer: "<<pEnd<<endl<<endl;
    > cout<<"Outside the function: "<<endl;
    > cout<<"Array size: "<<sizeof(a);
    > cout<<" Element size: "<<sizeof(a[0])<<endl;
    > cout<<"Pointer to array start: "<<a<<endl;
    > int numElements=sizeof(a)/sizeof(a[0]);
    > const double * pEnd2= a+numElements;
    > cout <<"Pointer to one-past-end: "<<pEnd2<<endl<<endl;
    > cout<<"Pointer to a[5]"<<&(a[5])<<endl;
    > return 0;
    > }
    >
    >
    > ....
    >
    > This still allows me to add extra 'magic numbers' in my const array at
    > the top of the file without changing any subsequent code. I'm not
    > entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
    > feels like the 'sizeof' should be redundant, but I understand why it
    > isn't.


    What about my solution?

    template<class T, int Size> T * endof( T (&parray)[Size]){
    return parray + Size;
    }
     
    Rolf Magnus, Jan 28, 2006
    #6
  7. shaun

    shaun roe Guest


    > > ....
    > >
    > > This still allows me to add extra 'magic numbers' in my const array at
    > > the top of the file without changing any subsequent code. I'm not
    > > entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
    > > feels like the 'sizeof' should be redundant, but I understand why it
    > > isn't.

    >
    > What about my solution?
    >
    > template<class T, int Size> T * endof( T (&parray)[Size]){
    > return parray + Size;
    > }


    Maybe I didnt properly understand, but isnt Size the number of elements?
    If I change the const array at the top of the file, I would have to
    change all my calls to reflect the new number of members.
     
    shaun roe, Jan 28, 2006
    #7
  8. shaun

    Kai-Uwe Bux Guest

    shaun roe wrote:

    >
    >> > ....
    >> >
    >> > This still allows me to add extra 'magic numbers' in my const array at
    >> > the top of the file without changing any subsequent code. I'm not
    >> > entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
    >> > feels like the 'sizeof' should be redundant, but I understand why it
    >> > isn't.

    >>
    >> What about my solution?
    >>
    >> template<class T, int Size> T * endof( T (&parray)[Size]){
    >> return parray + Size;
    >> }

    >
    > Maybe I didnt properly understand, but isnt Size the number of elements?
    > If I change the const array at the top of the file, I would have to
    > change all my calls to reflect the new number of members.



    In the syntax

    template<class T, int Size> T * endof( T (&parray)[Size]){
    return parray + Size;
    }

    the identifier "Size" is a template parameter. You will not need to change
    anything. Try the following on your compiler, which uses the same trick:

    template < typename T, unsigned long N >
    unsigned long length_of ( T const (& arg) [N] ) {
    return( N );
    }

    #include <iostream>

    const double a[] = { 2.3, 4.5, 6.6, 8.0, 10.0 };

    int main ( void ) {
    std::cout << length_of( a ) << '\n';
    }


    This should print 5. Once you change it to

    template < typename T, unsigned long N >
    unsigned long length_of ( T const (& arg) [N] ) {
    return( N );
    }

    #include <iostream>

    const double a[] = { 2.3, 4.5, 6.6, 8.0 };

    int main ( void ) {
    std::cout << length_of( a ) << '\n';
    }

    it will print 4. As you can see, no change to the template is needed. The
    compiler automatically deduces the correct value for Size (or N in my
    version) upon initialization of the template at the point where it is
    called.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 28, 2006
    #8
  9. shaun

    Rolf Magnus Guest

    shaun roe wrote:

    >
    >> > ....
    >> >
    >> > This still allows me to add extra 'magic numbers' in my const array at
    >> > the top of the file without changing any subsequent code. I'm not
    >> > entirely happy that I have to pass both 'a' and 'sizeof(a)' since it
    >> > feels like the 'sizeof' should be redundant, but I understand why it
    >> > isn't.

    >>
    >> What about my solution?
    >>
    >> template<class T, int Size> T * endof( T (&parray)[Size]){
    >> return parray + Size;
    >> }

    >
    > Maybe I didnt properly understand, but isnt Size the number of elements?


    Yes.

    > If I change the const array at the top of the file, I would have to
    > change all my calls to reflect the new number of members.


    No, you won't. You just call it like in your original posting. The number of
    elements is automatically deduced.
     
    Rolf Magnus, Jan 28, 2006
    #9
  10. shaun

    shaun roe Guest


    >
    > In the syntax
    >
    > template<class T, int Size> T * endof( T (&parray)[Size]){
    > return parray + Size;
    > }
    >
    > the identifier "Size" is a template parameter. You will not need to change
    > anything. Try the following on your compiler, which uses the same trick:
    >


    EXCELLENT! exactly what I wanted, many thanks!

    shaun
     
    shaun roe, Jan 28, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    424
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    489
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,283
  4. Amadeus W. M.
    Replies:
    2
    Views:
    414
    Amadeus W. M.
    Jul 4, 2006
  5. chhenning
    Replies:
    5
    Views:
    382
    chhenning
    Feb 13, 2008
Loading...

Share This Page