V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sl0000
V2EX  ›  C++

使用 typeinfo 获取类名称,实现简单字符串模版参数,这种用法有什么弊端吗?

  •  
  •   sl0000 · 2021-11-24 05:23:31 +08:00 · 1775 次点击
    这是一个创建于 876 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    #include <cxxabi.h>
    
    template <typename T>
    static inline const std::string classname() {
        int status;
        std::string actual_class_name = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
        if (status != 0) {
            throw "Class " + std::string(typeid(T).name()) + " parse error";
        } else {
            return actual_class_name;
        }
    }
    
    
    template <typename T>
    class kind {
        static inline const std::string n = classname<T>();
    };
    
    class dog { };
    name<dog> d;
    
    class cat { };
    name<cat> c;
    
    
    第 1 条附言  ·  2021-11-24 15:46:13 +08:00

    @GeruzoniAnsasu c++2a 可以使用字符串模版

    template<size_t N>
    struct StringLiteral {
        
        constexpr StringLiteral(const char (&str)[N]) {
            std::copy_n(str, N, value);
        }
        
        char value[N];
    };
    
    template<StringLiteral lit>
    void Print() {
        // The size of the string is available as a constant expression.
        constexpr auto size = sizeof(lit.value);
        // and so is the string's content.
        constexpr auto contents = lit.value;
        std::cout << "Size: " << size << ", Contents: " << contents << std::endl;
    }
    
    int main(int argc, const char * argv[]) {
        Print<"literal string">(); // Prints "Size: 15, Contents: literal string"
        return 0;
    }
    
    
    12 条回复    2021-11-24 12:58:10 +08:00
    ysc3839
        1
    ysc3839  
       2021-11-24 05:32:45 +08:00 via Android
    你要实现什么?不管父类被什么类继承,父类中的函数都能拿到子类的名称?
    dangyuluo
        2
    dangyuluo  
       2021-11-24 07:26:31 +08:00
    可以是可以,但是没看到你想要做什么😂代码最后你是想用 kind ,而不是 name 吧
    sl0000
        3
    sl0000  
    OP
       2021-11-24 09:18:20 +08:00
    @dangyuluo 对,这里写错了,应该是 kind<dog>, kind<cat>
    sl0000
        4
    sl0000  
    OP
       2021-11-24 09:22:45 +08:00
    @ysc3839 我用在其他动态语言的类包装上了,object<class_name>(Args ... args) 比如这样实例化一个 c#, js, 或者 java 类型
    nightwitch
        5
    nightwitch  
       2021-11-24 09:24:57 +08:00   ❤️ 1
    从此和 gcc 绑定了
    ysc3839
        6
    ysc3839  
       2021-11-24 09:25:30 +08:00 via Android
    @sl0000 没明白别的语言跟这个有什么关系。
    sl0000
        7
    sl0000  
    OP
       2021-11-24 09:42:47 +08:00
    @ysc3839 我在尝试用 c++实现一些动态语言的调用,kind 这个类会用 final 修饰。
    ysc3839
        8
    ysc3839  
       2021-11-24 09:57:32 +08:00 via Android
    @sl0000 所以动态语言调用以及 final 修饰和你给的这段代码有什么关系呢?
    sl0000
        9
    sl0000  
    OP
       2021-11-24 10:08:28 +08:00
    @ysc3839

    你问:"你要实现什么?"
    我答:“用来包装其他动态语言对象”
    你问:“不管父类被什么类继承,父类中的函数都能拿到子类的名称?”
    我答:“final”
    你问:“所以动态语言调用以及 final 修饰和你给的这段代码有什么关系呢?”
    我答:"js_object<js_class_name>(Args ... args) "
    ysc3839
        10
    ysc3839  
       2021-11-24 10:23:55 +08:00 via Android   ❤️ 1
    @sl0000 所以你是要获取某个类的名称字符串,然后传递给别的语言的引擎?
    应该是没有符合 C++标准的方法的,但是允许使用编译器特性的话,可以看看 https://github.com/Neargye/nameof
    nebkad
        11
    nebkad  
       2021-11-24 11:30:34 +08:00
    https://en.cppreference.com/w/cpp/types/type_info

    Defined in header <typeinfo>

    class type_info;


    The class type_info holds implementation-specific information about a type, including the name of the type and means to compare two types for equality or collating order. This is the class returned by the typeid operator.
    GeruzoniAnsasu
        12
    GeruzoniAnsasu  
       2021-11-24 12:58:10 +08:00   ❤️ 1
    大概理解目的是想用模板「魔法」出一个 c++类,这个类用起来是 c++但实际上在动态语言里产生作用

    那我只能说…… 给代码生产的迷惑性会远大于搞出来的那点点语法糖的甜度

    比如你好不容易让类的名称可以自动绑定了、类本身看起来成员能动态增改了,对象也能(很扭曲地) -> 调用动态塞进去的新方法了

    但不如构造函数里加个手动的 name 参数、手动访问 obj.members["fn"]、手动 vm->invoke(obj.members["fn"], ptr, args)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1266 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:54 · PVG 01:54 · LAX 10:54 · JFK 13:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.