Site icon Learn C++

What Are Integral_constant And () Operator In C++?

What Are Integral constant And Operator In C++

Modern C++ has base class features that can be used with other modern features of C++. The std::integral_constant is the base class for the C++ type traits in C++11, and in C++14, std::integral_constant gained an operator () overload to return the constant value. In this post, we explain what integral_constant and () operator are in C++14.

What is integral_constant in C++?

The std::integral_constant  is the base class for the C++ type traits in <type_traits> header that wraps a static constant of specified type. The behavior in a code part that adds specializations for std::integral_constant is undefined.

Here is the definition in <type_traits> header since C++11,

[crayon-673fb42b4b197664274383/]

Here is a very simple example to how can we use std::integral_constant, in C++11 we can use ::value to retrieve its value,

[crayon-673fb42b4b1a1980674723/]

What is the integral_constant () operator in C++?

In C++ 14, std::integral_constant gained an operator() overload to return the constant value that can be used with some other useful modern features like templates, lambdas, etc. In C++14, std::integral_constant gained an operator() overload to return the constant value. This () operator enables std::integral_constant to serve as a source of compile-time function objects.

In C++14, operator() is defined as shown below.

[crayon-673fb42b4b1a4046136449/]

Here is a simple example how we can use () operator in C++14 and above.

[crayon-673fb42b4b1aa837101802/]

Is there a full example of how to use integral_constant in C++?

Here is a full example about integral_constant in modern C++.

[crayon-673fb42b4b1ac087476362/]

and the output will be as below:

[crayon-673fb42b4b1b2858976571/]

Is there a full example of how to use the integral_constant () operator in C++?

Here is an example of the () operator of integral_constant, we define a myfactorial template struct that returns factorial value in compilation, which means there is no calculation about this operator on run time.

[crayon-673fb42b4b1b5640427803/]

and the output will be as we show below.

[crayon-673fb42b4b1b7986160435/]

For more details about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3545.pdf

C++ Builder is the easiest and fastest C and C++ compiler and 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 versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version