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.
Table of Contents
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,
1 2 3 |
template< class T, T v > struct integral_constant; |
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,
1 2 3 4 5 |
typedef std::integral_constant< int, 5 > five; std::cout << five::value << std::endl; // (C++11) |
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.
1 2 3 |
constexpr operator value_type() const noexcept; |
Here is a simple example how we can use () operator in C++14 and above.
1 2 3 4 5 6 |
typedef std::integral_constant< int, 5 > five; std::cout << "five = " << five::value << std::endl; // value (C++11) std::cout << "five = " << five() << std::endl; // operator() (C++14) |
Is there a full example of how to use integral_constant in C++?
Here is a full example about integral_constant
in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <type_traits> int main() { typedef std::integral_constant< int, 5 > five; typedef std::integral_constant< int, 10 > ten; std::cout << "five = " << five::value << std::endl; // value (C++11) std::cout << "five = " << five() << std::endl; // operator() (C++14) std::cout << "2*five = " << 2*five::value << std::endl; std::cout << "ten = " << ten::value << std::endl; system("pause"); return 0; } |
and the output will be as below:
1 2 3 4 5 6 |
five = 5 five = 5 2*five = 10 ten = 10 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <type_traits> template <unsigned n> struct myfactorial : std::integral_constant< int, n*myfactorial<n-1>() > { }; template <> struct myfactorial< 0 > : std::integral_constant< int, 1 > { }; int main() { constexpr int f5 = myfactorial<5>::value; // compiler coverts this int f5 = 120; std::cout << "Factorial 5 = " << f5 << std::endl; std::cout << "Factorial 6 = " << myfactorial<6>() << std::endl; // () operator returns constexpr (calculated in compilation not on runtime) system("pause"); return 0; } |
and the output will be as we show below.
1 2 3 4 |
Factorial 5 = 120 Factorial 6 = 720 |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition