#include <iostream>
#include <type_traits>
int main(){
//creating an integral constant with constexpr
constexpr unsigned int speed_of_light{299792458};
//creating an integral constant with std::integral_constant
typedef std::integral_constant<unsigned int, 299792458> speed_of_light_2;
//using them
std::cout << speed_of_light/2 << '\n';
std::cout << speed_of_light_2::value/2 << '\n';
}
What's special about std::integral_constant
that I would choose to use it over constexpr
?
Their behaviour and use cases look identical to me. I'm trying to think of some kind of template scenario, where constexpr may not suffice.
std::integral_constant
to be for making integral constants. It's a TMP feature used for various kinds of things, especially tag dispatching (usually based on its specializationsstd::true_type
andstd::false_type
), something a constant value cannot be used for. Of course for constants use actual constants and not some strange typified constant.