RajSharma
RajSharma

Reputation: 1971

What is the use of typedef here?

I am having difficulty to figure out what is the use typedef here-

typedef char TYPE_SSOSettingError;
typedef void (*ans_executeDomainRegistration) (TYPE_SSOSettingError);

From the first line I understand that TYPE_SSOSettingError is defined as char.

From the next line I can figure out that ans_executeDomainRegistrationis a pointer to a function which is having return type of void and takes parameter of type char which in this situation is TYPE_SSOSettingError

Then what is the use of typedef in the last line?

Upvotes: 3

Views: 246

Answers (8)

Niall
Niall

Reputation: 30605

The typedef is, as you noted, a pointer to a function. So it can be used to declare said pointer to said function and then bind it to an actual pointer to a function with said signature and then called as desired.

typedef char TYPE_SSOSettingError;
typedef void (*ans_executeDomainRegistration) (TYPE_SSOSettingError);
// ...
void somefunc(TYPE_SSOSettingError);
// ...
ans_executeDomainRegistration ptr = &somefunc;

The typical use case for this is with a callback function of some sort. It is used in the standard library for setting the terminate function etc. std::set_terminate, with the terminate handler typedef.

typedef void (*terminate_handler)();
std::terminate_handler set_terminate( std::terminate_handler f );

In more general terms, use of the typedef (and type aliases using) provides a technique to offer a better abstraction.

void (*ptr) (char) = &somefunc; // 1
ans_executeDomainRegistration ptr = &somefunc; // 2

Your case in point, it is unclear in line 1 what ptr will be used for, line 2 offers clearer intent, the ptr will be used as a function call for the execution of a domain registration and that function accepts an SSO settings error.

It is a technique to make the code easier to read, better, terse, or more succinct in order to express the concepts in the code (for some definition of easier, better etc.). Use them well; when not used well, they can also make the code more obscure, and harder to read and understand.

Upvotes: 6

Frodo
Frodo

Reputation: 749

You are right this is a typedef of a function pointer with return value void and parameter TYPE_SSOSettingError.

The typedef can be used to increase the readability of your code when using a function pointer.

Function pointer declarations

For example declaring a function pointer fct_ptr1 and fct_ptr2 which are exactly the same:

// Type definition:
typedef void (*ans_executeDomainRegistration) (TYPE_SSOSettingError);

// Declarations:
ans_executeDomainRegistration fct_ptr1 = foo;    // With typedef
void (*fct_ptr2) (TYPE_SSOSettingError) = foo;   // Without typedef

Function declarations

Also if you have any function with a function pointer as parameter:

// Write:
void SomeFct(ans_executeDomainRegistration param) {...}        // With typedef
// Instead of:
void SomeOtherFct(void (*param)(TYPE_SSOSettingError)) {...}   // Without typedef

Conclusion

So as a result of using the typedef you see it is more familiar to declare pointers or functions if you have declared a type for the function pointer.

Upvotes: 3

songyuanyao
songyuanyao

Reputation: 172934

I suppose you've known about typedef, and you're asking about why to use defined typedef at 2nd typedef.

Then what is the use of typedef in the last line.

It's guaranteed that if TYPE_SSOSettingError gets changed, the parameter's type of ans_executeDomainRegistration will get changed too.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

In C++ 11 you can use an alias declaration instead of the typedef that looks more clear.

For example

using ans_executeDomainRegistration = void ( * )( TYPE_SSOSettingError );

From this declaration it is seen that ans_executeDomainRegistration is a name for type void ( * )( TYPE_SSOSettingError ). That is this name denotes the type of pointer to function with one parameter and return type void.

So instead of writing in the program for example like

typedef char TYPE_SSOSettingError;
void myFunc( TYPE_SSOSettingError );

//...
void (*myFuncPtr1) (TYPE_SSOSettingError) = myFunc;
void (*myFuncPtr2) (TYPE_SSOSettingError) = myFunc;

you can write

ans_executeDomainRegistration myFuncPtr1 = myFunc;
ans_executeDomainRegistration myFuncPtr2 = myFunc;

Upvotes: 1

Shooter
Shooter

Reputation: 392

Think of a function which returns a function pointer of type ans_executeDomainRegistration.

Imagine something like:

ans_executeDomainRegistration getDomainRegistrationFunction(enum DomainType d)
{
  return global_ans_executeDomainRegistration[getDomainindex(d)];
}

which is called like:

(*getDomain(MY_DOMAIN))(varSSOSettingError);

Upvotes: 1

Johannes
Johannes

Reputation: 6717

The second typedef makes it so you have a defined type for later use.

Here an example to contrast a use with and without typedef.

#include <stdio.h>

typedef char TYPE_SSOSettingError;
typedef void(*ans_executeDomainRegistration) (TYPE_SSOSettingError);

void myprint(TYPE_SSOSettingError c)
{
    printf("%c\n", c);
}

int main()
{
    ans_executeDomainRegistration with_typedef = myprint;
    void(*without_typedef) (TYPE_SSOSettingError) = myprint;

    with_typedef('w');
    without_typedef('o');
}

Upvotes: 2

bolov
bolov

Reputation: 75727

To make more clear declarations like this:

int register_callback(void (*execute) (TYPE_SSOSettingError));

vs

int register_callback(ans_executeDomainRegistration* execute));

Also, to make it clear what a function pointer should be.

Upvotes: 4

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42909

typedef void (*ans_executeDomainRegistration) (TYPE_SSOSettingError);

typedefs a function pointer to a function of type void(char).

Example:

void foo(char) {
  ...
}

int main() {
  ans_executeDomainRegistration f = &foo;

  f('a');
}

Upvotes: 2

Related Questions