I have seen declaring a reference variable as constant in C++ on Quora.
static constexpr const int& r = 3;
So, Why both constexpr
and const
used in single statement?
What is the purpose of that type of statement?
I have seen declaring a reference variable as constant in C++ on Quora.
static constexpr const int& r = 3;
So, Why both constexpr
and const
used in single statement?
What is the purpose of that type of statement?
const
variables are ones that cannot be modified after initialisation (e.g. const int a = 1
).
constexpr
variables are constant expressions and can be used at compile-time. The use of constexpr for a variable declaration implies const.
However, in this declaration, const
applies to the int, while constexpr
applies to const int&
(a reference to a const int).