1

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).

tohava
  • 5,344
  • 1
  • 25
  • 47
  • you want to make sure that your function will not compile? – levengli Jul 15 '13 at 11:30
  • It's unclear what it is that you want. Do you want `x = y` to generate a compilation error ? Something else ? Do you want to only impact the `f` function ? Or should this be some global behaviour ? How does using `const void*` achieve that ? – Sander De Dycker Jul 15 '13 at 11:33

2 Answers2

3

typedef is more suitable here.

For immutable data: typedef const void * T;

For immutable pointer: typedef void * const T;

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
0

I made a dumb mistake. Apparently,

typedef const int T 

or

typedef void * const T

work fine for preventing the assignment.

tohava
  • 5,344
  • 1
  • 25
  • 47