Search code examples
cgenericstypestypeof

Establish a single data type with typeof


I want to make a program that works with only one specified data type. I would establish, with typeof, the type of all data in my program according the first data received.

For example, in this case:

int main() {    
    float a = 5;
    typeof(a) var1;
    typeof(b) var2;
    //etc...
}

But if I want to do it with an extern function contained in a library?

Example:

int main() {
    void* var1 = create_var("hello");  //with the first call 'create_var()' establish the data type: in this case -> string
    void* var2 = create_var("c");     //now i want that this is allowed
    void* var3 = create_var(2);      //and this is not allowed

How can i do this function?


Solution

  • By the time you write the framework to support your create_var function, you will have invented a new language, one which supports runtime type checking (instead of C's compile/link-time type checking) and templates. That's what happened with C++ (it was originally a set of preprocessor macros for C).

    It may be easier to start with a dynamically-typed language and add your own type enforcement to your data types and functions.

    Examples of previous work: