xhawk18 / promise-cpp

C++ promise/A+ library in Javascript style.
MIT License
679 stars 92 forks source link

请问可否在不支持rtti的环境中运行? #15

Closed Cirnoo closed 2 years ago

Cirnoo commented 2 years ago

看到代码中用到了一些typeid的判断,我们的项目由于包大小的原因禁用了rtti

xhawk18 commented 2 years ago

typeid没办法去掉,因为里面用到 any 类型,类型转换倚赖 typeid 做判断

Cirnoo commented 2 years ago

我尝试给anyHolder生成一个id替换掉rtti的typeid~~

xhawk18 commented 2 years ago

比如类似这样创建一个type_id? 不清楚这个办法是否足够健壮。。。

代码更新 https://github.com/xhawk18/promise-cpp/commit/df0544fcf0eca024b3e4a6068051216d11d8c97b

#include <iostream>

template<typename T>
ptrdiff_t type_id() {
    static char idHolder;
    return (ptrdiff_t)&idHolder;
}

int main() {

    std::cout << type_id<int>() << "\n";
    std::cout << type_id<char>() << "\n";
    std::cout << type_id<decltype(main)>() << "\n";
    std::cout << type_id<decltype(main)>() << "\n";

    return 0;
}
Cirnoo commented 2 years ago

抱歉才看到,感谢~