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).
const is redundant" Hardly.constexpr no longer implies const.const from the code in the question. It won't compile.const if you also change the initializer. My point stands: the answer was wrong to say the const was redundant, as removing the const changed the meaning.
constexprapplies to the reference andconst intis the thing referred to, they have different meanings