Function Template Question

Discussion in 'C++' started by Dennis Pais, Jul 28, 2004.

  1. Dennis Pais

    Dennis Pais Guest

    Here's a piece of code that I found in C++ Primer by Stan Lippman !

    -------------------------------------------------
    template<typename T, int size>
    T min(T (&r_array) [size] )
    {
    Type min_value = r_array[0];

    for(int i=1; i<size; i++)
    {
    if( r_array < min_value)
    min_value = r_array;
    }

    return min_value;
    }

    int ia[] = {1,2,3,4,5,6};

    //to invoke the above function
    int main()
    {
    int i = min(ia);
    printf("Hello World");
    }
    -------------------------------------------------

    I am working with a Microsoft compiler (VC++ 6.0), & the above code
    fails to compile with the following error(s):
    --------------------------------------------------------------
    E:\TEMPLATES\Templates\Templates.cpp(91) : error C2265: '<Unknown>' :
    reference to a zero-sized array is illegal
    E:\TEMPLATES\Templates\Templates.cpp(124) : error C2784: 'T __cdecl
    min(T (&)[1])' : could not deduce template argument for ' (&)[1]' from
    'int [5]'
    Error executing cl.exe.
    ---------------------------------------------------------------

    Can someone tell me if the above code works with some other
    non-Microsoft compiler & why the Microsoft compiler does not allow
    this code to execute ?

    -Thanks
    Dennis
     
    Dennis Pais, Jul 28, 2004
    #1
    1. Advertising

  2. * Dennis Pais:
    >
    > Here's a piece of code that I found in C++ Primer by Stan Lippman !
    >
    > -------------------------------------------------
    > template<typename T, int size>


    Should be 'size_t', not 'int'.


    > T min(T (&r_array) [size] )


    Efficiency: should return 'T const&' or type dependent on T.


    > {


    Here should be assertion or check of size > 0.


    > Type min_value = r_array[0];


    This should be

    T const* min_value = &r_array[0];

    and other use of 'min_value' adjusted accordingly.


    > for(int i=1; i<size; i++)


    Style: preferentially use ++i, not i++.


    > {
    > if( r_array < min_value)
    > min_value = r_array;


    Style: always use brackets for the 'if' body (and others).

    > }
    >
    > return min_value;
    > }


    This is less then ideal design. The 'min' function of array ref
    argument should better forward to one with array + length or
    begin+end pointers arguments. That would make it much more useful.


    > int ia[] = {1,2,3,4,5,6};
    >
    > //to invoke the above function
    > int main()
    > {
    > int i = min(ia);
    > printf("Hello World");


    Style: preferentially use type-safe C++ iostreams for small test
    programs -- efficiency is not an issue, but clarity and safety is.

    > }
    > -------------------------------------------------
    >
    > I am working with a Microsoft compiler (VC++ 6.0), & the above code
    > fails to compile


    MSVC 6.0 is a very limited and not very standard-compliant.

    Try a better compiler.

    In effect, just about any other compiler, including MSVC 7.x.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 28, 2004
    #2
    1. Advertising

  3. Dennis Pais

    tom_usenet Guest

    On 28 Jul 2004 05:07:25 -0700, (Dennis Pais)
    wrote:

    >Here's a piece of code that I found in C++ Primer by Stan Lippman !
    >
    >-------------------------------------------------
    >template<typename T, int size>
    >T min(T (&r_array) [size] )
    >{
    > Type min_value = r_array[0];
    >
    > for(int i=1; i<size; i++)
    > {
    > if( r_array < min_value)
    > min_value = r_array;
    > }
    >
    > return min_value;


    A simpler implementation would be:
    return *std::min_element(r_array, r_array + size);
    >}
    >
    >int ia[] = {1,2,3,4,5,6};
    >
    >//to invoke the above function
    >int main()
    >{
    > int i = min(ia);
    > printf("Hello World");
    >}
    >-------------------------------------------------
    >
    >I am working with a Microsoft compiler (VC++ 6.0), & the above code
    >fails to compile with the following error(s):
    >--------------------------------------------------------------
    >E:\TEMPLATES\Templates\Templates.cpp(91) : error C2265: '<Unknown>' :
    >reference to a zero-sized array is illegal
    >E:\TEMPLATES\Templates\Templates.cpp(124) : error C2784: 'T __cdecl
    >min(T (&)[1])' : could not deduce template argument for ' (&)[1]' from
    >'int [5]'
    >Error executing cl.exe.
    >---------------------------------------------------------------
    >
    >Can someone tell me if the above code works with some other
    >non-Microsoft compiler & why the Microsoft compiler does not allow
    >this code to execute ?


    The code should work. MSVC has problems with templates in general. I
    think the specific problem here is that it can't deduce array bounds
    from arguments passed to template functions. There may be a
    workaround, but I haven't got MSVC6 installed to experiment with.

    Tom
     
    tom_usenet, Jul 28, 2004
    #3
  4. * Alf P. Steinbach:
    > * Dennis Pais:
    > >
    > > Here's a piece of code that I found in C++ Primer by Stan Lippman !
    > >
    > > -------------------------------------------------
    > > template<typename T, int size>

    >
    > Should be 'size_t', not 'int'.
    >
    >
    > > T min(T (&r_array) [size] )

    >
    > Efficiency: should return 'T const&' or type dependent on T.


    Forgot to mention: the argument should be 'T const (&r_array) [size]'.


    > > {

    >
    > Here should be assertion or check of size > 0.


    Forgot to mention context: when the thing is redesigned as suggested
    below.


    > > Type min_value = r_array[0];

    >
    > This should be
    >
    > T const* min_value = &r_array[0];
    >
    > and other use of 'min_value' adjusted accordingly.
    >
    >
    > > for(int i=1; i<size; i++)

    >
    > Style: preferentially use ++i, not i++.
    >
    >
    > > {
    > > if( r_array < min_value)
    > > min_value = r_array;

    >
    > Style: always use brackets for the 'if' body (and others).
    >
    > > }
    > >
    > > return min_value;
    > > }

    >
    > This is less then ideal design. The 'min' function of array ref
    > argument should better forward to one with array + length or
    > begin+end pointers arguments. That would make it much more useful.
    >
    >
    > > int ia[] = {1,2,3,4,5,6};
    > >
    > > //to invoke the above function
    > > int main()
    > > {
    > > int i = min(ia);
    > > printf("Hello World");

    >
    > Style: preferentially use type-safe C++ iostreams for small test
    > programs -- efficiency is not an issue, but clarity and safety is.
    >
    > > }
    > > -------------------------------------------------
    > >
    > > I am working with a Microsoft compiler (VC++ 6.0), & the above code
    > > fails to compile

    >
    > MSVC 6.0 is a very limited and not very standard-compliant.
    >
    > Try a better compiler.
    >
    > In effect, just about any other compiler, including MSVC 7.x.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 28, 2004
    #4
    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. Chris Theis
    Replies:
    2
    Views:
    478
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    538
    tom_usenet
    Jul 24, 2003
  3. Robert Allan Schwartz
    Replies:
    1
    Views:
    399
    Victor Bazarov
    Aug 9, 2004
  4. Jim West
    Replies:
    3
    Views:
    764
    Jim West
    Oct 7, 2004
  5. Peng Yu
    Replies:
    3
    Views:
    781
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page