1

I understand why I can't cast a const to non const, but this is the inverse:

    float** inputChannelData = buffer.getArrayOfWritePointers();
    simpleRecorder->processInput(inputChannelData, buffer.getNumChannels(), buffer.getNumSamples());

signature:

void SimpleRecorder::processInput(const float * *inputChannelData, int numInputChannels, int numSamples)

Error:

Cannot initialize a parameter of type 'const float **' with an lvalue of type 'float **'

Why it even complies? I can pass something not const to a const.

I tried

reinterpret_cast<const float **>(inputChannelData)

but I get Reinterpret_cast from 'float **' to 'const float **' casts away qualifiers. What qualifiers? I'm actually putting a qualifier, not taking it out.

Rafaelo
  • 33
  • 1
  • 15
  • For adding (or removing) the `const` *qualifier* you need to use `const_cast`. – Some programmer dude Jun 17 '21 at 06:52
  • @Someprogrammerdude but why the compiler even complains? It's not dangerous to pass a non const thing where a const thing is expected – Rafaelo Jun 17 '21 at 06:56
  • It really depends on what is really `const`... There a real difference between `const float**` and `float* const *`. `float**` can be implicitly converted to `float* const*`, but not `const float**` (or `float const**` which might make it easier to understand)- – Some programmer dude Jun 17 '21 at 07:00
  • To rephrase it a little: Only the first level of indirection can be converted to `const`. So if you have `float*` then it can be converted to `float const*`. If you have `float**` then it can be converted to `float* const*`. If you have `float***` then it can be converted to `float** const*`. Etc. – Some programmer dude Jun 17 '21 at 07:05
  • What is the return type of `getArrayOfWritePointers`? Why can't you use it directly in the `processInput` call? Why do you need the (temporary?) variable `inputChannelData`? – Some programmer dude Jun 17 '21 at 07:10

1 Answers1

3

Since a const float** is a pointer to a non-const const float* (!), you can write a const float* to it:

const float f = 0.0f;
const float *p = &f; // Not itself const, points to one.
const float ** pp = &p

Obviously, a float** can't point to a const float*. If the cast were possible, you could sneak a const float* in a float**, and use that float** to then overwrite a const float

MSalters
  • 173,980
  • 10
  • 155
  • 350