Function definition using typedef function in C ++

I use the service on Windows. VisualStudio 2012 has the following typedef function:

typedef VOID WINAPI SERVICE_MAIN_FUNCTIONW ( DWORD dwNumServicesArgs, LPWSTR *lpServiceArgVectors ); 

There is also a typedef function pointer:

 typedef VOID (WINAPI *LPSERVICE_MAIN_FUNCTIONW)( DWORD dwNumServicesArgs, LPWSTR *lpServiceArgVectors ); 

How to define a function using this function using typedef?

+4
source share
4 answers

You do not need (and cannot) use typedef in a function definition. To make the main function of the service, simply write:

 VOID WINAPI SvcMain( DWORD dwNumServicesArgs, LPWSTR *lpServiceArgVectors ) { // ... } 

LPSERVICE_MAIN_FUNCTIONW used internally in Windows to call each service start point. Typically, you need a typedef function pointer to call, not to define a function.

+1
source

Quoting the current C ++ standard (C ++ 11):

[dcl.fct] / 10

A typical function type can be used to declare a function, but should not be used to define a function (8.4). [Example:

 typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void fv() { } // OK: definition of fv 

-end example]

That is, you can declare, but not define a function using typedef . You must explicitly state the signature, see Alex Farber Response.


To some extent, you can use typedef to β€œdefine” a function, but it includes template magic. This is just a fun example showing that you can use it to define a function.

 // extra definitions for SSCCE typedef unsigned int DWORD; typedef wchar_t* LPWSTR; #define VOID void #define WINAPI // function ptr typedef VOID (WINAPI *LPSERVICE_MAIN_FUNCTIONW)( DWORD dwNumServicesArgs, LPWSTR *lpServiceArgVectors ); // function typedef typedef VOID WINAPI SERVICE_MAIN_FUNCTIONW ( DWORD dwNumServicesArgs, LPWSTR *lpServiceArgVectors ); template < typename... TT > struct disassemble_funcptr {}; template < typename Ret, typename... Args > struct disassemble_funcptr < Ret(Args...) > { typedef Ret return_type; static Ret Func(Args...) { /* your code here */ } }; // using the typedef SERVICE_MAIN_FUNCTIONW to define the function LPSERVICE_MAIN_FUNCTIONW my_func_ptr = & disassemble_funcptr < SERVICE_MAIN_FUNCTIONW > :: Func; int main() { LPWSTR str = nullptr; my_func_ptr(42, &str); } 
+6
source

A function cannot be defined (or declared) using a typedef . It should be determined in the usual way. eg.

 void foo (type1 t1, type2 t2) // t1 and t2 are objects which can be ... { // ... visible only in conventional way ... } 

The function pointer typedef can declare a function pointer, which can be assigned with the address of the function:

 typedef void (*PF)(type1, type2); PF pf = &foo; 
+1
source

With C ++ 11, you can use the fact that lambdas with empty capture lists are converted to function pointers for declaring and defining a function that matches the typedef function in a single expression, for example:

 using my_function_t = void(*)(int,int); my_function_t add_function = [](int x, int y) { return x + y; }; 
0
source

Source: https://habr.com/ru/post/1479472/


All Articles