Is there a gcc command line option to turn off the warning: passing argument n discards qualifiers from type

I am trying to compile -Wall -Werror and its compression of my style.

I try to make it clear that some arguments are constants, and then pass them to the non-invert qualification functions inside a large library.

PS I basically did this, trying to make it clear that certain variables are constants, is this good or bad c-style for this when working with library functions that don't use const?

+3
source share
3 answers

, . , ""? , , ?

, , , , , , gcc, . , .

privelege, 9 . C .

+1

-Wno-ignored-qualifiers.

, -Wall -Wextra -Werror ( , ), , . , : -Wno-long-long. , , , .

, , , const . :

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-qualifiers"

OffendingThirdPartyFunction(MyConstParam);

#pragma GCC diagnostic pop

(, , , GCC )

#define NO_WARNING(expr)                                        \
    _Pragma("GCC diagnostic push")                              \
    _Pragma("GCC diagnostic ignored \"-Wignored-qualifiers\"")  \
    expr                                                        \
    _Pragma("GCC diagnostic pop")

NO_WARNING(OffendingThirdPartyFunction(MyConstParam));

, . , , .

OffendingThirdPartyFunction((param_t*)MyConstParam);
+1

: , . , , , , .

, , .

, , -const, , const-ness . , () , , , , .

:

// in this library
void print_int(int *p) { printf("%d\n", *p); }
void set_int(int *p) { *p = 6; }

// your code
const int n = 5;
print_int((int*)(&n)); // warning suppressed for this call only,
                       // having carefully checked the print_int docs.

// further down your code
set_int(&n); // You *want* the compiler to stop this!

, ( ), :

void print_int_const(const int *p) { print_int((int*)(p)); }
// but no wrapper for set_int

, volatile ( ). , .

0

Source: https://habr.com/ru/post/1794688/


All Articles