473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

a sizeof problem about template

hi,all I encounter a problem with sizeof

template <typename type>
void display(type& a)
{
cout<<sizeof(type)<<":"<<a[0]<<endl;
}

int main()
{
int a[6] = {1,2,3,4,5,6};
display(a);
}

the output is 24:1, its means is sizeof(type) equals 24,but the type
here is int
24 is the sizeof array a,why?Thanks

Jul 28 '06 #1
2 4955
hu*******@gmail.com wrote:
hi,all I encounter a problem with sizeof

template <typename type>
void display(type& a)
{
cout<<sizeof(type)<<":"<<a[0]<<endl;
}

int main()
{
int a[6] = {1,2,3,4,5,6};
display(a);
}

the output is 24:1, its means is sizeof(type) equals 24,but the type
here is int
No, it's array[6] of int.
24 is the sizeof array a,why?Thanks
You pass an array to display, which expects a reference to some type. So you
are getting a reference to the array. Within display, 'type' is the type of
that array.

Jul 28 '06 #2
hu*******@gmail.com posted:
hi,all I encounter a problem with sizeof

template <typename type>
void display(type& a)
{
cout<<sizeof(type)<<":"<<a[0]<<endl;
}

int main()
{
int a[6] = {1,2,3,4,5,6};
display(a);
}

the output is 24:1, its means is sizeof(type) equals 24,but the type
here is int
24 is the sizeof array a,why?Thanks

"type" is int[6], not int.

Two remedies:

(1) Use: sizeof *a

or:

(2) Change the template to the following:

#include <cstddef>
#include <iostream>

template<class T,std::size_t len>
void Display(T const (&arr)[len])
{
std::cout << "Size of type: " << sizeof(T)
<< " Quantity: " << len
<< " Total Size: " << sizeof arr << '\n';
}

template<class T>
void Display(T const &obj)
{
Display(reinterpret_cast<T const (&)[1]>(obj));
}

int main()
{
int obj;

int arr[5];

Display(obj);
Display(arr);
}

--

Frederick Gotham
Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Metzen | last post by:
hello, ok, I want to find the length of an int array that is being passed to a function: main() { int array={1,2,3,4,5,6,7,8}; function(array); } function(int *array) {
10
by: | last post by:
How can I write this correctly? #if sizeof(int) < sizeof(my_int) .............. #endif I want compile different pieces of code for different platforms
11
by: ajikoe | last post by:
Hello I have this code: void sizeArr(float a){ cout << sizeof(a) << endl; } void test(void){ float val = {1, 2, 0.1}; sizeArr(val); // return 4
19
by: Abubakar | last post by:
Hi, lets say I have the following class: class AAA{}; if I do: sizeof(AAA) it gives me 1. Why is the size 1 ? Regards,
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
11
by: nevergone | last post by:
Hello Everybody In <<Modern C++ Design>Compile-Time Assertions there is : template <boolstruct CompileTimeChecker { CompileTimeChecker(...); }; template <struct CompileTimeChecker<false{ };
42
by: Jess | last post by:
Hello, if I have the following code that has an array of int*: #include<iostream> #include<string> #include<cstring> #include<cstddef> using namespace std; int main(){
9
by: Faisal | last post by:
Hi, Why C++ doesn't allow overloading of size of operator. I think it would be much handy to check the type userdefined types. For eg. In my project, I've some structures which contains...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.