LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2006, 11:36 AM   #1
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
C++ template and sizeof


Whilst trying to break introuble post I came across something which I can not explain.
Enums are used alot in templates and the following struct baffles me
Code:
template<int size>
struct Amount
{
	enum{ value = size};
};
Now an enum is just an int or some type which is big enough to hold the value. Is this correct?

See if I do a size of
Code:
...
sizeof(Amount<60>)
...
The result is 1 byte so this got me thinking a char is being used; but if a sizeof is done on Amount<60>::value then 4 bytes is returned (size of an int). This is where I am really confused, the value inside the struct is bigger than the struct itself.

I then give the "size" a bigger value than what a char can hold (Amount<500>) and the same results were returned.

Have I just completely missed something here, or is this correct?



edit:
The results are the same for Kdevelop and Visual Studio so I am therefore assuming that it is correct and I have missed something.
Any ideas?

Last edited by dmail; 08-02-2006 at 12:38 PM. Reason: added info
 
Old 08-03-2006, 01:32 AM   #2
mldqj
LQ Newbie
 
Registered: Apr 2004
Location: CT, US
Distribution: Ubuntu
Posts: 24

Rep: Reputation: 15
There is nothing wrong. You defined an enum inside a struct. That enum is a nested type instead of a member variable of the struct. Therefore, struct Amount is empty in the sense that it does not hold any data.
 
Old 08-03-2006, 08:01 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Thanks for that mldqj.
By adding a data member to the struct I see the nested enum has no size at all.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof problem ltordsen Programming 8 12-05-2005 09:13 AM
sizeof and malloc in C exvor Programming 4 08-07-2005 12:06 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
sizeof( ) in c++ question amoor757 Programming 6 01-26-2004 02:42 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration