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 4050568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:05:10+00:00 2026-05-20T14:05:10+00:00

Possible Duplicate: Use templates to get an array’s size and end address Can someone

  • 0

Possible Duplicate:
Use templates to get an array’s size and end address

Can someone explain this template code that gives me the size of an array? (the first answer includes obtaining the value as a compile time constant)

How to get size of an array using metaprogramming? Multidimensional would be also appreciated.
So for example if I pass a type to this struct (how ever it’s called, let’s say get_dim) I would get:

get_dim<int>::value; //0
get_dim<int[1]>::value //1
get_dim<int[2][3]>::value //2,3
  • 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-20T14:05:10+00:00Added an answer on May 20, 2026 at 2:05 pm

    For one dimensional Arrays,

    template<typename T, size_t N>
    size_t size_of_array(T (&)[N])
    {
       return N;
    }
    
    int arr[]={1,2,2,3,4,4,4,54,5};
    cout << size_of_array(arr) << endl;     
    
    A a[] = {A(),A(), A(), A(), A()};
    cout << size_of_array(a) << endl;
    

    Output:

    9
    5
    

    Full Demo at Ideone : http://ideone.com/tpWk8


    EDIT:

    Another way (after seeing your edit),

    template<typename T>
    struct get_dim;
    
    template<typename T, size_t N>
    struct get_dim<T[N]>
    {
        static const int value = N;
    };
    
    int main() 
    {
           cout << get_dim<int[100]>::value;
           return 0;
    }
    

    Output:

    100
    

    http://ideone.com/wdFuz


    EDIT:

    For two dimensional arrays:

    struct size2D
    {
       size_t X;
       size_t Y;
    };
    
    template<typename T, size_t M, size_t N>
    size2D size_of_array(T (&)[M][N])
    {
       size2D s = { M, N};
       return s;
    }
    int arr[][5]={ {1,2,2,5,3}, {4,4,4,54,5}} ;
    size2D size = size_of_array(arr);
    cout << size.X <<", "<< size.Y << endl; 
    

    Output:

    2, 5
    

    Code : http://ideone.com/2lrUq

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

Sidebar

Related Questions

Possible Duplicate: How to put braces in django templates? When I use jQuery template
Possible Duplicate: C++: Why can't I use float value as a template parameter? I
Possible Duplicate: How to use enable_if to enable member functions based on template parameter
Possible Duplicate: Can we use any other TAG inside <ul> along with <li>? Or
Possible Duplicate: How to use local classes with templates? g++ 4.4 is refusing to
Possible Duplicate: Why use a framework with PHP? To begin with, I just can
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: Use of class definitions inside a method in Java Can we have
Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up
Possible Duplicate: Absence of typeof operator in C++03? Is it possible to use templates

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.