Skip to content

C23 typeof examples? #28

@dramforever

Description

@dramforever

typeof is available in C23 as well as GCC and Clang as extensions (might need __typeof__ depending on flags)

See:

I'm not sure how to organize this into the existing examples though. Here's my attempt at using typeof on everything in the C section, with original as comparison. Some of these aren't really better.

Note that I haven't tested these.


As a variable:

returnType (*variableName)(parameterTypes) = function_name;
typeof(returnType (parameterTypes)) *variableName = function_name;

As a static const variable:

static returnType (* const variableName)(parameterTypes) = function_name;
static typeof(returnType (parameterTypes)) * const variableName = function_name;

As an array:

returnType (*arrayName[])(parameterTypes) = {function_name0, ...};
typeof(returnType (parameterTypes)) *arrayName[] = {function_name0, ...};

As a parameter to a function:

int my_function(returnType (*parameterName)(parameterTypes));
int my_function(typeof(returnType (parameterTypes)) *parameterName);

As a return value from a function:

returnType (*my_function(int, ...))(parameterTypes);
typeof(returnType (parameterTypes)) *my_function(int, ...);

As a cast (but try not to cast functions):

... (returnType (*)(parameterTypes))my_expression ...
... (typeof(returnType (parameterTypes)) *)my_expression ...

As a function pointer typedef:

typedef returnType (*typeName)(parameterTypes);
typedef typeof(returnType (parameterTypes)) *typeName;

As a function typedef:

typedef returnType typeName(parameterTypes);
typedef typeof(returnType (parameterTypes)) typeName;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions