sl0000
V2EX  ›  C++

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

  •  
  •   sl0000 · Nov 24, 2021 · 2497 views
    This topic created in 1635 days ago, the information mentioned may be changed or developed.
    
    #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;
    
    
    Supplement 1  ·  Nov 24, 2021

    @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 replies    2021-11-24 12:58:10 +08:00
    ysc3839
        1
    ysc3839  
       Nov 24, 2021 via Android
    你要实现什么?不管父类被什么类继承,父类中的函数都能拿到子类的名称?
    dangyuluo
        2
    dangyuluo  
       Nov 24, 2021
    可以是可以,但是没看到你想要做什么😂代码最后你是想用 kind ,而不是 name 吧
    sl0000
        3
    sl0000  
    OP
       Nov 24, 2021
    @dangyuluo 对,这里写错了,应该是 kind<dog>, kind<cat>
    sl0000
        4
    sl0000  
    OP
       Nov 24, 2021
    @ysc3839 我用在其他动态语言的类包装上了,object<class_name>(Args ... args) 比如这样实例化一个 c#, js, 或者 java 类型
    nightwitch
        5
    nightwitch  
       Nov 24, 2021   ❤️ 1
    从此和 gcc 绑定了
    ysc3839
        6
    ysc3839  
       Nov 24, 2021 via Android
    @sl0000 没明白别的语言跟这个有什么关系。
    sl0000
        7
    sl0000  
    OP
       Nov 24, 2021
    @ysc3839 我在尝试用 c++实现一些动态语言的调用,kind 这个类会用 final 修饰。
    ysc3839
        8
    ysc3839  
       Nov 24, 2021 via Android
    @sl0000 所以动态语言调用以及 final 修饰和你给的这段代码有什么关系呢?
    sl0000
        9
    sl0000  
    OP
       Nov 24, 2021
    @ysc3839

    你问:"你要实现什么?"
    我答:“用来包装其他动态语言对象”
    你问:“不管父类被什么类继承,父类中的函数都能拿到子类的名称?”
    我答:“final”
    你问:“所以动态语言调用以及 final 修饰和你给的这段代码有什么关系呢?”
    我答:"js_object<js_class_name>(Args ... args) "
    ysc3839
        10
    ysc3839  
       Nov 24, 2021 via Android   ❤️ 1
    @sl0000 所以你是要获取某个类的名称字符串,然后传递给别的语言的引擎?
    应该是没有符合 C++标准的方法的,但是允许使用编译器特性的话,可以看看 https://github.com/Neargye/nameof
    nebkad
        11
    nebkad  
       Nov 24, 2021
    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  
       Nov 24, 2021   ❤️ 1
    大概理解目的是想用模板「魔法」出一个 c++类,这个类用起来是 c++但实际上在动态语言里产生作用

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

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

    但不如构造函数里加个手动的 name 参数、手动访问 obj.members["fn"]、手动 vm->invoke(obj.members["fn"], ptr, args)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1082 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 154ms · UTC 17:52 · PVG 01:52 · LAX 10:52 · JFK 13:52
    ♥ Do have faith in what you're doing.