Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to un-const typeof in gcc pure C?

Tags:

c

gcc

macros

I have a macro that uses GCC's typeof to create a variable of the same type of a macro argument. The problem is: if that argument has const type, the variable created inside the macro is const and I can't use it. For instance:

#include <stdio.h>

#define DECR(x) ({typeof(x) y; y = x; y--; y;})

int main(void)
{
    const int v = 5;
    printf("%d\n", DECR(v));
    return 0;
}

Compilation gives:

$ cc    -c -o t.o t.c
t.c: In function 'main':
t.c:9:2: error: assignment of read-only variable 'y'
t.c:9:2: error: decrement of read-only variable 'y'
make: *** [t.o] Error 1

Is there a way to copy the typeof a value and un-const it?

like image 526
Penz Avatar asked Aug 05 '13 16:08

Penz


People also ask

Can you modify a const variable?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

What is __ typeof __ in C?

The __typeof__ operator returns the type of its argument, which can be an expression or a type. The language feature provides a way to derive the type from an expression. Given an expression e , __typeof__(e) can be used anywhere a type name is needed, for example in a declaration or in a cast.

What does typeof return in C?

In other languages, such as C# or D and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions), the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form.


2 Answers

If you don't mind the possible arithmetic promotion you can do this:

#define DECR(x) ({typeof(x + 0) y; y = x; y--; y;})

The trick is that the expression for typeof is x + 0, which is a r-value, and so the l-value-constness (which is what you want to avoid) is lost.

The same trick can be done with 1 * x, but curiously enough, +x and -x don't work.

like image 60
rodrigo Avatar answered Sep 27 '22 23:09

rodrigo


This is a rather late answer, but if you don't mind using more GCC extensions you can do this like this (building upon a previous answer somewhat).

#define UNCONST_HAX_(TYPE) ({TYPE _tmp_macro_var_; _tmp_macro_var_;})
#define UNCONST(x)                                                      \
    __typeof__(_Generic((x),                                            \
            signed char:              UNCONST_HAX_(signed char),        \
            const signed char:        UNCONST_HAX_(signed char),        \
            unsigned char:            UNCONST_HAX_(unsigned char),      \
            const unsigned char:      UNCONST_HAX_(unsigned char),      \
            short:                    UNCONST_HAX_(short),              \
            const short:              UNCONST_HAX_(short),              \
            unsigned short:           UNCONST_HAX_(unsigned short),     \
            const unsigned short:     UNCONST_HAX_(unsigned short),     \
            int:                      UNCONST_HAX_(int),                \
            const int:                UNCONST_HAX_(int),                \
            unsigned:                 UNCONST_HAX_(unsigned),           \
            const unsigned:           UNCONST_HAX_(unsigned),           \
            long:                     UNCONST_HAX_(long),               \
            const long:               UNCONST_HAX_(long),               \
            unsigned long:            UNCONST_HAX_(unsigned long),      \
            const unsigned long:      UNCONST_HAX_(unsigned long),      \
            long long:                UNCONST_HAX_(long long),          \
            const long long:          UNCONST_HAX_(long long),          \
            unsigned long long:       UNCONST_HAX_(unsigned long long), \
            const unsigned long long: UNCONST_HAX_(unsigned long long), \
            float:                    UNCONST_HAX_(float),              \
            const float:              UNCONST_HAX_(float),              \
            double:                   UNCONST_HAX_(double),             \
            const double:             UNCONST_HAX_(double),             \
            long double:              UNCONST_HAX_(long double),        \
            const long double:        UNCONST_HAX_(long double)         \
    ))

And it could be used as follows:

#define DECR(x) ({UNCONST(x) y; y = x; y--; y;})

Yes, it is pretty ugly.

like image 28
Roflcopter4 Avatar answered Sep 27 '22 22:09

Roflcopter4