I am trying to convert “int32_t” to “static const int32_t” type. However, I could not figure out how to use static_cast and const_cast together. Any help would be appreciated.
I want to do this so that rather than initializing my “static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2” to a hardcore value, I would like to set this based on value passed to the relevant function.
Say, the value of iNoOfSamples_In is 128, I would like to set IRF_MAX_ENVELOPE_ELEMENTS2 to 128 too; but, as as a “static const int32_t” like this:
Do not use a cast in contexts where it is not required;unnecessary casts engender brittle, error-prone code.
1 2 3 4 5 6 7 8 9 10 11
void foo( std::int32_t num_samples )
{
// *** warning *** static storage duration
// *** max_envelope_elements will be initialised once
// *** the first time when the function is called
// *** is this what was intended?
staticconst std::int32_t max_envelope_elements = num_samples ;
// note: static const auto max_envelope_elements = num_samples ;
// would be code that is more flexible, maintainable
}
Actually, I was hesitaant at using this as I am using a struct defined in the third party library and I was not sure whether I can use new keyword. But, it seemed working even there.
sSample* myRFPulseArray = new sSample[IRF_MAX_ENVELOPE_ELEMENTS2];