开发者

Function definition and function type

I have the following code which 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 defining x and define p there straight. Something like

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

Is that possible? If so how do I do it? If not why?

EDIT:

After seeing the answers, I think I should clarify: I want the definition to be separate. i.e. function definition will be in a shared object. An application will acquire a function pointer to the function via dlsym. I do not want a function object. What I want is to know if I can define a function using its type which a header file common to both the shared object and the application will provide. I hope that came out right :).

EDIT2: For sbi :)

This resides in a header which is included in both the application and 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)();

In the application, something like this happens:

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

Now, to do this, I have to define GetFactory as follows in the shared object:

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

Forgetting the extern "C" part for now, Can I define this function using开发者_运维问答 the type TpfGetFactory which is already defined? (This is not a huge issue I know - but I am curious as to whether it is possible :) ). What I want is something like this in the shared object :

TpfGetFactory f;
f(){//Implementation}

EDIT3:

My try:

#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


It's possible in C++1x, the next C++ standard, generally expected next year (which would make it C++11, then). It allows this:

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

This relies on auto been given a new meaning ("automatically deduce the type of this variable from the expression that initializes it") and the new lambda functions (allowing to create functions on the fly).

Your compiler might actually already support this.


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 maybe it happens to be more complicated to write than what you wanted to simplify ;-), however it works and you can define your functions in place, locally.

Here is some documentation about local classes


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


Not directly in C++98.

For standard C++, that is C++98, check out e.g. the Boost Lambda library. It lets you write expressions like

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

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

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜