cast “int32_t” to “static const int32_t”

Hello,

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:

1
2
	int32_t iNoOfSamples_In	= 128;
	static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2 = 128;


My half attempt to convert to static cast:

static int32_t IRF_MAX_ENVELOPE_ELEMENTS3 = static_cast <int32_t> (iNoOfSamples_In);

What I would like to do actually is this:

static const int32_t IRF_MAX_ENVELOPE_ELEMENTS4 = convert_to_static_const_int32_t(iNoOfSamples_In);

Any help would be appreciated.

Regards,
Dushyant
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?
    static const 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
}
Hi JLBorges,

Even if I do what you suggested, I still can not declare an array like this

double dTime_Scale[max_envelope_elements]; // Line 80

I get the following errors (line 80 marked in code snippet):

1
2
3
SpecialPulses.cpp(80) : error C2057: expected constant expression
SpecialPulses.cpp(80) : error C2466: cannot allocate an array of constant size 0
SpecialPulses.cpp(80) : error C2133: 'dTime_Scale' : unknown size


So, it seems that max_envelope_elements is not constant
Last edited on
Never mind, I could easily solve this.

double* dTime_Scale = new double[IRF_MAX_ENVELOPE_ELEMENTS2];

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];

Thanks JLBorges.

Regards,
DK
Last edited on
Topic archived. No new replies allowed.