Function definition and function type

I have the following code that works as expected:

#include <iostream> using namespace std; typedef int (TMyFunc)(int); TMyFunc* p; int x(int y) { return y*2; } int main() { p = &x; cout << (*p)(5) << endl; } 

What I want to do is skip the definition of x and define p there directly. Sort of

TMyFunc p; p(y){return y*2;} TMyFunc p; p(y){return y*2;} .

Is it possible? If so, how do I do this? If not?

EDIT:

After looking at the answers, I think I should clarify: I want the definition to be separate. that is, the definition of the function will be in a common object. The application will receive a function pointer through dlsym . I do not want a function object. I want to know if I can define a function using its type, which will contain a header file that is common to both a common object and an application. Hope this worked out right :).

EDIT2: for sbi :)

This is in the header, which is included both in the application and in the shared object:

 #define FNAME_GET_FACTORY "GetFactory" #define FNAME_GET_FUNCTION_IDS "GetFunctionIDs" #define FNAME_GET_PLUGIN_INFO "GetPluginInfo" typedef FunctionFactory* (*TpfGetFactory)(); typedef size_t (*TpfGetFunctionIDs)(int**); typedef PluginInfo* (*TpfGetPluginInfo)(); 

Something like this happens in the application:

 TpfGetFactory pF = (TpfGetFactory)dlsym(pHandle, FNAME_GET_FACTORY); //Use pF for anything 

Now for this I have to define GetFactory as follows in a generic object:

 extern "C" FunctionFactory* FNAME_GET_FACTORY(){//CODE} 

Forget the extern "C" for now. Can I define this function using the already installed TpfGetFactory type? (This is not a huge problem that I know, but I'm curious if this is possible :)). I want something like this in a shared object:

 TpfGetFactory f; f(){//Implementation} 

EDIT3:

My attempt:

 #include <iostream> using namespace std; typedef int (TF)(int); TF f; f(int x) { return x*2; } int main() { x(3); } main.cpp:9: error: ISO C++ forbids declaration of Γ’β‚¬ΛœfÒ€ℒ with no type main.cpp: In function Γ’β‚¬Λœint main()Ò€ℒ: main.cpp:16: error: Γ’β‚¬ΛœxÒ€ℒ was not declared in this scope 
+1
source share
4 answers

This is possible in C ++ 1x, the next C ++ standard commonly expected next year (which will make it C ++ 11, then). This allows:

 auto p = [](int y){return y*2;}; 

This relies on auto , which is given a new value ("automatically infers the type of this variable from the expression that initializes it") and new lambda functions (allowing you to create functions on the fly).

Your compiler may already support this.

+4
source

This works fine for me, with the current C ++ 03 standard:

 typedef int (TMyFunc)(int); TMyFunc* p; int test() { struct LocalClass { static int functionLocal(int y) { return 2; }; }; LocalClass localClass; p = &(LocalClass::functionLocal); } 

But it may be harder to write than what you wanted to simplify ;-), however, it works, and you can define your functions locally.

Below is the documentation for local classes

+2
source

This will be possible in the next C ++ standard via lambdas. In the current standard, however, it is not possible to define one function inside another.

0
source

Not directly in C ++ 98.

For standard C ++, i.e. C ++ 98, check for example. in the Boost Lambda library. It allows you to write expressions like

 for_each(a.begin(), a.end(), std::cout << _1 << ' '); 

C ++ 0x adds direct support for lambda expressions.

Cheers and hth.,

0
source

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


All Articles