C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

What Is An Extended sizeof In Modern C++?

What Is An Extended sizeof In Modern C++

In modern programming, the size of any data is very important, while the programmers are trying to minimize data types or stream packages, global data transfer is increasing by the requests of new technologies. Thus, we need to know each data type in programming. The sizeof() function is very useful to get the size of variables in bytes. C++11 extends the functionality of sizeof function so that class or class members can be sent as parameters even if no object has been instantiated. In this post we explain with examples, what is the sizeof function, what is the extended sizeof() function, and how does C++11 extend the sizeof() operator on classes?.

What is the sizeof() function in C++?

The sizeof() function is very useful to get the size of variables in bytes. Generally, size of data packages ( structs, classes, objects, arrays) need to be checked. When you create a struct or class, and If you are using them in I/O operations, check how much size it is.

Syntaxes for the sizeof operator is,

and,

Checking the size of data is sometimes important in professional programming. These checks allow you to optimize your data packages thus your application use a lower amount of memory and it will have more efficient and thus higher speed I/O operations. Game data packages, video and audio packages, TCP/IP data packages, AI related data packages are these kinds of packages.

Is there a simple example of sizeof operator in C++??

Here is a simple sizeof() example,

What is the extended sizeof operator in modern C++?

C++11 extends the functionality of sizeof function so that class or class members can be sent as parameters even if no object has been instantiated. You can access to the members of a class by using the :: specifier. For example, we can get the size of a member in a class as below.

Note that this member should be a public member.

Is there an extended sizeof operator in modern C++?

We can check the size of different class types as shown below.

and the output will be as below:

Are there more examples of the extended sizeof operator in modern C++?

Here is an example how you can use extended sizeof with the classes inside namespaces:

Here is another example that we can get size of member of a template, or size of an object member created by a template.

For more information on this feature, see Extending sizeof Proposal document.

What Is An Extended sizeof In Modern C++ The C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?