简体   繁体   English

有没有办法使用RTTI获取基于签名的typeinfo损坏的函数名?

[英]Is there a way to get a signature based typeinfo mangled function name using RTTI?

I want to use RTTI and mangled function (pointer) type strings. 我想使用RTTI和错误的函数(指针)类型的字符串。

Before you categorize this as an XY problem, I'm aware that there are better options to bind functions using polymorphism etc. 在将其归类为XY问题之前,我知道有更好的选择来使用多态性绑定函数。

This is a purely academical question how to use typeid() properly with function pointers that should differ by their legally overloaded signatures. 这是一个纯粹的学术性问题,如何将typeid()与函数指针正确使用,该函数指针应因其法律重载签名而有所不同。

If I use the following code , it seems I can retrieve unique typeinfo::name() values for various namespaces/types: 如果使用以下代码 ,似乎可以为各种名称空间/类型检索唯一的typeinfo::name()值:

#include <iostream>
#include <typeinfo>
#include <string>

void foo(int) {
}

namespace woozle {
    void goozle(int) {}
}

struct bar {
    void baz(int) {}
    static void boo(int) {}
};

int main() {
    std::cout << typeid(&foo).name() << std::endl;
    std::cout << typeid(&woozle::goozle).name() << std::endl;
    std::cout << typeid(&bar::baz).name() << std::endl;
    std::cout << typeid(&bar::boo).name() << std::endl;
}

The output is: 输出为:

 PFviE PFviE M3barFviE PFviE 

Perfectly what I expected (I assume the i in the mangled name refers to the parameter signature). 完全符合我的期望(我认为错误名称中的i表示参数签名)。


Now I want to have something like this (which is perfectly legal function overloading): 现在,我想要这样的东西(完全合法的函数重载):

#include <iostream>
#include <typeinfo>
#include <string>

void foo(int) {
}

void foo(std::string) {
}

namespace woozle {
    void goozle(int) {}
    void goozle(std::string) {}
}

struct bar {
    void baz(int) {}
    static void boo(int) {}
    void baz(std::string) {}
    static void boo(std::string) {}
};

int main() {
    std::cout << typeid(&foo).name() << std::endl;
    std::cout << typeid(&woozle::goozle).name() << std::endl;
    std::cout << typeid(&bar::baz).name() << std::endl;
    std::cout << typeid(&bar::boo).name() << std::endl;
}

and of course the compiler complains about ambiguity : 当然, 编译器会抱怨模棱两可

 main.cpp: In function 'int main()': main.cpp:24:25: error: address of overloaded function with no contextual type information std::cout << typeid(&foo).name() << std::endl; ^~~~ main.cpp:25:25: error: address of overloaded function with no contextual type information std::cout << typeid(&woozle::goozle).name() << std::endl; ^~~~~~~ main.cpp:26:25: error: address of overloaded function with no contextual type information std::cout << typeid(&bar::baz).name() << std::endl; ^~~~ main.cpp:27:25: error: address of overloaded function with no contextual type information std::cout << typeid(&bar::boo).name() << std::endl; ^~~~ 

TL;DR TL; DR

What is the proper syntax to specify a specific function overload with typeid() (if there is any)? typeid()指定特定函数重载的正确语法是什么(如果有)?

How can I provide the "contextual type information" that is demanded from the error message? 如何提供错误消息中要求的“上下文类型信息”


I'm coming from here in deep thinking mode. 我从这里进入深度思考模式。

To select a specific function from a set of overloaded functions you can use use the cast notation: 要从一组重载函数中选择一个特定的函数,可以使用强制转换符号:

std::cout << typeid(static_cast<void (*)(int)>(foo)).name() << std::endl;
std::cout << typeid(static_cast<void (*)(std::string)>(foo)).name() << std::endl;
std::cout << typeid(static_cast<void (bar::*)(int)>(&bar::baz)).name() << std::endl;
std::cout << typeid(static_cast<void (bar::*)(std::string)>(&bar::baz)).name() << std::endl;

Specifically with typeid though, if you have the type already written down, you can skip the actual function name. 但是,特别是对于typeid,如果已经记录了类型,则可以跳过实际的函数名称。

std::cout << typeid(void (*)(int)).name() << std::endl;

is shorter and does the job just as well. 较短,工作也一样好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM