Search code examples
cassignment-operatorstrong-typingnoncopyable

Prevent assignment of a type in C


Given a C function:

void f(T x, T y) {
    x = y;
}

I want to make sure that all instances of T assignments will fail. So far, the best solution I have is something like:

#define T const void *

is there a better solution? (Ideally I would like T to be defined as some kind of a non-assignable opaque record pointer type).


Solution

  • typedef is more suitable here.

    For immutable data: typedef const void * T;

    For immutable pointer: typedef void * const T;