rhlee
rhlee

Reputation: 4046

Runtime conditional typedef in C

I know there is a C++ version of this question, however I'm using standard typedefs not templates.

I've written a program that works with 16-bit wav files. It does this by loading each sample into a short. The program then performs arithmetic on the short.

I'm now modifying the program so it can with with both 16- and 32-bit wavs. I was hoping to do a conditional typedef, i.e. using short for 16-bit and int for 32-bit. But then I realised that the compiler probably would not compile the code if it did not know what the type of a variable is beforehand.

So I tried to test out the following code:

#include <stdio.h>

int
main()
{
  int i;
  scanf("%i", &i);

  typedef short test;

  if(i == 1)
    typedef short sample;
  else 
    typedef int sample;

  return 0;
}

And got got the following compiler errors:

dt.c: In function ‘main’:
dt.c:12:5: error: expected expression before ‘typedef’
dt.c:14:5: error: expected expression before ‘typedef’

Does this mean that runtime conditional typedefs in C are not possible?

[Open-ended question:] If not, how would you guys handle something like this?

Upvotes: 2

Views: 6559

Answers (5)

md5
md5

Reputation: 23699

On the other hand, you can use typedef only in some scope.

int 
main(void) {
    if (1) {
        typedef short sample;
        sample n; // OK
    }
    sample u; // ERROR
    return 0;   
}

Upvotes: 2

ecatmur
ecatmur

Reputation: 157344

All types in a program must be known at compile time.

In C++ you could compile your code for short and int using templates; in C you can do this using macros (specifically, X-macros).

Put your calculation code in a separate file called e.g. dt.tmpl.c, then in dt.c write:

#define sample int
#include "dt.tmpl.c"

#define sample short
#include "dt.tmpl.c"

Your dt.tmpl.c code can then use sample as a preprocessor token to name types and paste into function names, for example:

#define PASTE(name, type) name ## _ ## type
#define FUNCTION_NAME(name, type) PASTE(name, type)

sample FUNCTION_NAME(my_calculation, sample)(sample i) {
    return i * 2;
}

This will result in two functions int my_calculation_int(int i) and short my_calculation_short(short i) which you can then use elsewhere.

Upvotes: 3

AndersK
AndersK

Reputation: 36082

first of all, a typedef is not a new type, it is an alias or a short form to make things more convenient (especially when working with function pointers)

C is a static language, you cannot create a type during runtime, the type needs to be resolved at compile/link time.

well if it were possible the windows API would be sooo much smaller :)

in windows they have two versions of functions for most every API call and a define that decides which to use.

e.g.

#ifndef UNICODE
#define myfunction _myfunctionA(TCHAR* p);
#else
#define myfunction _myfunctionW(TCHAR* p);
#endif

but again, the type is decided at compile time

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753655

The types in C (and C++) are fixed at compile time; the concept of a 'runtime typedef' does not make sense in C.

However, your problem is solvable with care. You will define two sets of functions (and types), one to deal with 16-bit .wav files and one to deal with 32-bit .wav files. These will be defined at compile time, and the sizes of the types will be fixed at compile time, but both lots of code will be in the executable, and you'll be able to choose which set of functions to execute based on run-time information (the type of file you're asked to process, or produce).

Your two sets of functions should be symmetric. You might be able to use some 'template-like' mechanism so that you compile the same file twice, once for the 32-bit code, once for the 16-bit code, ensuring that the function names are changed consistently.

Upvotes: 0

MByD
MByD

Reputation: 137312

typedef is a compiler feature, you cannot apply it on runtime.

Upvotes: 5

Related Questions