Thread: template function issue

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    template function issue

    Hello everyone,


    In the following code, how GetArrayLength(arr1) is matched to template function size_t GetArrayLength(const T(&arr)[size])? My confusion is how arr1 is matched to const T(&arr)[size]? I have tried to change const T(&arr)[size] to const T(arr)[size]) but it does not work.

    Code:
    template<size_t size, typename T> 
    size_t GetArrayLength(const T(&arr)[size])
    {
    	return size;
    }
    
    int main()
    {
      char arr1[] = "Hello World";
      std::cout << GetArrayLength(arr1) << std::endl;
    
      return 0;
    }

    thanks in advance,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would guess that if you pass a non-reference, the array decays to a pointer and the size argument is just ignored (for one dimension).

    If you pass a reference to an array, its size needs to be known and can be deduced by the compiler.

    Code:
    #include <iostream>
    
    void foo(const char c[10]) 
    {
        
        std::cout << c << '\n';
    }
    
    void bar(const char (&c)[10])
    {
        std::cout << c << '\n';
    }
    
    int main()
    {
        char ten[] = "ABCDEFGHI";
        char twelve[] = "Hello world";
        foo(ten);
        foo(twelve); //number of characters doesn't matter
        bar(ten);
        //bar(twelve); //ERROR, expected char[10], got char[12]
    }
    Edit: if your compiler has the __PRETTY_FUNCTION__ macro, you might see that the first signature is seen as void foo(const char* c)
    Last edited by anon; 12-12-2007 at 04:39 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The variable is just a placeholder which allows the important information -- the value of size -- to flow in. You could replace it with: T(&)[size] if you wanted:

    Code:
    template<size_t size, typename T> 
    size_t GetArrayLength(const T(&)[size])
    {
    	return size;
    }
    This ONLY WORKS for real arrays, i.e. where the length is actually known to the compiler.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Save yourself some trouble with this, though. You can use std::string and not have to worry about size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Save yourself some trouble with this, though. You can use std::string and not have to worry about size.
    This is applicable to far more than just strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM