Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:58+00:00 2026-05-27T07:20:58+00:00

Possible Duplicate: Can someone explain this template code that gives me the size of

  • 0

Possible Duplicate:
Can someone explain this template code that gives me the size of an array?

template <typename T,unsigned S>
unsigned ArraySize(const T (&v)[S])
{
    return S;
}

I understand the T and S, but my question is why do we have to declare v as a reference variable? Or maybe I’m misunderstanding this whole thing.

I appreciate the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:20:59+00:00Added an answer on May 27, 2026 at 7:20 am

    The function accepts an array by reference, and because of this, type of element T and size S is deduced by the compiler. So it returns S which is nothing but the size of the array. In the absence of reference, it would decay into a pointer type. So there is no difference between these:

    void f(int v[100]);  //declaration of f
    void f(int v[200]);  //redeclaration of f
    void f(int v[]);     //redeclaration of f
    void f(int *v);      //redeclaration of f
    

    All are exactly same. You can pass array of any size to all of these functions.


    Coming back to ArraySize, the returned value of this function cannot be used as constant expression:

    int a[10];
    SomeClassTemplate<ArraySize(a)> obj; //error
    

    See error : http://ideone.com/4mdJE

    So a better implementation would be this:

    template <typename T,unsigned S>
    char (&ArraySizeHelper(const T (&v)[S]))[S];  //no need to define it!
    #define ArraySize(a) sizeof(ArraySizeHelper(a))
    

    Now this is perfectly fine:

    int a[10];
    SomeClassTemplate<ArraySize(a)> obj; //ok
    

    See ok : http://ideone.com/Zt3UY

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: Javascript parseInt gives very unexpected results Can someone explain why javascript interprets
Possible Duplicate: In jQuery, what does $.fn. mean? Can someone explain the code below
Possible Duplicate: What's a queryString doing in this stylesheet's href? Can someone explain the
Possible Duplicate: Direct casting vs 'as' operator? Can someone explain the difference to me
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: How does the new automatic reference counting mechanism work? Can someone explain
Possible Duplicate: XML attribute vs XML element Can someone explain me why more and
Possible Duplicate: Datatable vs Dataset Can someone please explain the difference to me or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.