Thread: A very strange looking template

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    A very strange looking template

    My brain can't quite figure out how to parse this template:
    Code:
    template< typename T, std::size_t N > inline
    std::size_t size( T(&)[N] ) { return N ; }
    Found here: http://www.daniweb.com/forums/thread95690.html

    Specifically, the T(&)[N] is what's really got me confused...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by cpjust View Post
    My brain can't quite figure out how to parse this template:
    Code:
    template< typename T, std::size_t N > inline
    std::size_t size( T(&)[N] ) { return N ; }
    Found here: http://www.daniweb.com/forums/thread95690.html

    Specifically, the T(&)[N] is what's really got me confused...
    Just gonna throw in my guess here...

    It probably has to do with precedence. Without the parentheses and brackets it could be a function call with argument &N even if it doesn't make sense to us the compiler could see it that way. That way () and [] are to make sure that &N is a reference to type T... I tried to compile without the parentheses and brackets and VC++ didn't like that at all.

    But, that's just a guess I could be totally off base.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The parens surrounding the & make sure that what you're passing is an array by reference and not an array of references. This is done because you cannot pass the array directly, as it would decay to a pointer in the parameter list. Only by passing it by reference can you preserve the array and, more importantly, enforce extent checking.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Ronix View Post
    The parens surrounding the & make sure that what you're passing is an array by reference and not an array of references. This is done because you cannot pass the array directly, as it would decay to a pointer in the parameter list. Only by passing it by reference can you preserve the array and, more importantly, enforce extent checking.
    Well, the important thing in this particular case is gaining access to N (the size of the array). If it decayed to a pointer you'd lose that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I was fairly certain that function definitions, whether or not they were templates, required named parameters. This example doesn't meet that, and yet it compiles. I learn new things all the time.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by whiteflags View Post
    I was fairly certain that function definitions, whether or not they were templates, required named parameters. This example doesn't meet that, and yet it compiles. I learn new things all the time.
    A parameter only needs a name, well... if it needs one. If the parameter is never used, it does not need a name, and sometimes SHOULD not have a name, to prevent compiler warnings about unused variables.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM