获取被调用函数的名称

发布于 2024-09-12 21:18:30 字数 775 浏览 9 评论 0原文

当使用“.”、“->”时或“->*”运算符是否有任何方法可以获取调用的函数的名称(或在“->*”的情况下获取变量的名称,一切顺利。

编辑: 只是为了澄清,我不是在谈论反射,而是在“函数”行中谈论更多内容,基本上我想创建一个运算符的重载(最好是提到的三个之一),它知道运算符的右侧

我想要完成的基本上是编译时转换为

obj->Function(a,b);

obj->Map("Function")(a,b); //Where Map(string) returns a function pointer

编辑:只是为了进一步澄清,要求是代码必须看起来 就像这三个之一一样,

obj.SomeMethod(args);
obj->SomeMethod(args);
obj->*SomeMethod(args);

任何适合的解决方案都是可以接受的,只要 SomeMethod 可以是任何有效的标识符(即任何可以用作函数名称的东西),并且该解决方案必须使用 SomeMethod 作为映射中的查找。地图部分已经实现,它只是对我寻求解决方案的方法调用的重新解释。

如果这(我担心)无法完成,那么任何具有新“运算符”语法的解决方案都将被接受。如 obj __ SomeMethod(args);只要查找是基于“操作员”的 RHS

when using either the '.', '->' or '->*' operators is there any way of getting the name of the function invoked (or the name of the variable in the case of '->*' and everything goes.

EDIT:
Just to clarify I'm not talking about reflection but more something in the line of 'function' basically I want to create an overload of an operator (preferrably one of the three mentioned) which is aware of the right side of the operator

What I'm trying to accomplish is basically a compile time transformation of

obj->Function(a,b);

to

obj->Map("Function")(a,b); //Where Map(string) returns a function pointer

EDIT: Just to further clarify, The requirement is that the code will have to look like one of these three

obj.SomeMethod(args);
obj->SomeMethod(args);
obj->*SomeMethod(args);

any solution that fits that is acceptable as long as SomeMethod can be any valid identifier (I.e. any thing that can be used as a function name) and the solution will have to use SomeMethod as a lookup in a Map. The Map part is already implemented it's only the reinterpretation of what looks like a method call I seeking a solution to.

if that (as I'm affraid) can't be done then any solution with a new "operator" syntax will be accepted. Such as obj __ SomeMethod(args); as long as the lookup is based on the RHS of the "operator"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春花秋月 2024-09-19 21:18:31

当使用“.”、“->”时或“->”运算符是否有任何方法可以获取调用的函数的名称(或者在“->”的情况下获取变量的名称,一切顺利。

没有构建-in 机制(我知道)允许

在编译时使用 __FUNCTION__ 宏来获取当前函数的名称,这不是标准的,但它是。 MSVC 支持,并且可能在 g++ 上支持(不确定)

如果您想在运行时知道函数名称,那么您必须为此实现某种特殊机制 - 构建一个函数名称和地址表(其中)。将在启动时初始化),并在每次需要从地址中获取函数名称时搜索它。如果编译器支持 __FUNCTION__ 宏或有任何其他方式获取当前函数的名称,那么您可以简化。通过使用宏(您可以将整个“注册”过程包装到单个宏调用中,该宏调用将放置在您想要注册的函数中)。这可能是不可移植的。

程序的调试信息也可能包含您正在查找的信息,但这比依赖 __FUNCTION__ 宏更糟糕。

when using either the '.', '->' or '->' operators is there any way of getting the name of the function invoked (or the name of the variable in the case of '->' and everything goes.

There is no built-in mechanism (i know of) that allows that.

You can get a name of current function during compile time, using __FUNCTION__ macro. It is not standard, but it is supported on MSVC, and may be supported on g++ (not sure).

If you want to know function name during runtime, then you'll have to implement some kind of special mechanism for that - build a table of function names and addresses (which will be initialized at startup) and search it for function name every time you need a function name from address. If compiler supports __FUNCTION__ macros or have any other means of getting name for current function, then you can simplify that by using macros (you can wrap entire "registration" procedure into single macro call that will be placed within function you want to register). This probably will be non-portable.

Program's debug information may also contain information you're looking for but it is even worse than relying on __FUNCTION__ macro.

一向肩并 2024-09-19 21:18:31

我刚刚解决了您最近面临的问题。我正在将 Javascript 绑定到 C++,并且需要有一个名称为 -> 的函数映射。功能。我像这样调用函数:

vector<WebValue> params;  // Parameters for the function, I get these from the JS engine usually
params.push_back(asValue("Some string"));
params.push_back(asValue(123456));
params.push_back(asValue(1.0f));
functionMap["SomeFunction"]->call(params);  // SomeFunction must accept char*, int and float compatible parameters

并像这样注册它:

functionMap["SomeFunction"] = jsExport(this, &MyClass::SomeFunction);

或者像这样,如果它是一个成员函数并且“this”在当前作用域中可用:

regMember(SomeFunction);

我使用 Boost Fusion 和 Boost Function Types 来为我完成这项肮脏的工作。它可以在 OpenLieroX 项目的源代码中找到:
http://bit.ly/dd70G6

它的用法:
http://bit.ly/9Sa84q - 包含将函数注册到地图的方法
http://bit.ly/b7oWNo - 包含用于注册成员函数的方便的 regMember 宏,

您可能需要查看为了更好地理解,还可以查看目录中的其他文件:
http://bit.ly/b7oWNo

这与您在问题中描述的方式不完全一样,但希望如此对你有帮助。

I have solved the problem you are facing just recently. I was doing Javascript bindings to C++ and needed to have a map of functions with name -> function. I am calling the functions like this:

vector<WebValue> params;  // Parameters for the function, I get these from the JS engine usually
params.push_back(asValue("Some string"));
params.push_back(asValue(123456));
params.push_back(asValue(1.0f));
functionMap["SomeFunction"]->call(params);  // SomeFunction must accept char*, int and float compatible parameters

and registering it like this:

functionMap["SomeFunction"] = jsExport(this, &MyClass::SomeFunction);

or like this if it's a member function and "this" is available in the current scope:

regMember(SomeFunction);

I used Boost Fusion and Boost Function Types to do the dirty job for me. It's available under the sources of OpenLieroX project:
http://bit.ly/dd70G6

The usage of it:
http://bit.ly/9Sa84q - contains methods for registering the functions to the map
http://bit.ly/b7oWNo - contains the handy regMember macro for registering member functions

You may want to look at other files in the directory as well for a better understanding:
http://bit.ly/b7oWNo

It is not exactly the way you described it in your question but hopefully it helps you.

许仙没带伞 2024-09-19 21:18:31

不。C++ 几乎没有反射。除非您希望将其用于调试目的,否则您实际上并不需要它。即使没有它也可以进行调试。

编辑的回答:要么使用某种具有多个重载operator()函数的函子,要么您的映射必须仅包含相同类型的函数,或者至少包含以下类型的函数:相同的参数和返回值(如果您愿意使用诸如 boost::function 之类的东西)。如果您将函子与重载的operator()一起使用,那么您将放弃编译时类型安全性,并且您将受到一些N个参数的限制。

您决定执行的操作类似于 boost::bind/boost::function。看看他们的标题。除非您想单独限制您的实现,否则您最终不可能得到比他们使用的更简单的东西。

No. C++ has nearly no reflection. Unless you want this for debugging purposes you should not really need it. And debugging can be done even without it.

Answer to the EDIT: Either you use some kind of functor with several overloaded operator() functions or your map will have to contain only functions of same type, or at least of same parameters and return value (if you are willing to use something like boost::function<>). If you use functor with overloaded operator() then you are giving up compile time type safety and you will be limited to some N parameters.

What you have decided to do is akin to boost::bind/boost::function. Take a look at their headers. Unless you want to severally limit your implementation, it is unlikely you can end up with anything simpler than they use.

深空失忆 2024-09-19 21:18:30

据我所知,没有。 C++ 不支持任何形式的反射,因此为了获取此类信息,您必须设计自己的工具和技巧。

As far as I know, no. C++ does not support any form of reflection, so in order to get this kind of information you have to design your own tools and tricks.

如歌彻婉言 2024-09-19 21:18:30

C++ 没有任何类型的反射。当可执行文件运行时,函数的名称就消失了。唯一的方法就是将它们传入。

如果您打算根据方法名称执行某些操作,也许您只需要虚拟方法或重载,以便调用正确的代码。

C++ doesn't have any kind of reflection. The names of the functions are gone by the time that your executable is running. The only way to do it is to pass them in.

If you were planning to do something based on the method name, perhaps you just need virtual methods or overloading so that the right code is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文