Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Casting const void* const to const char* const produces the "ignored-qualifiers" warning, why?

I don’t understand why the following code gives the "type qualifiers ignored on cast result type" warning in GCC, can you please explain?

#include <stdint.h>

char f(const void* const a) noexcept {
    return static_cast<char>(*static_cast<const char* const>(a));
}

https://godbolt.org/z/n8Ws9aTcq

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A non-class non-array prvalue cannot be cv-qualified.

This means that your static_cast<const char* const>(a) is exactly the same as static_cast<const char*>(a), the pointer itself isn’t const-qualified. It’s simply discarded, which is what the warning is telling you about (the second const is what is being ignored)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading