
#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; @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; } 1 ysc3839 2021-11-24 05:32:45 +08:00 via Android 你要实现什么?不管父类被什么类继承,父类中的函数都能拿到子类的名称? |
2 dangyuluo 2021-11-24 07:26:31 +08:00 可以是可以,但是没看到你想要做什么代码最后你是想用 kind ,而不是 name 吧 |
4 sl0000 OP @ysc3839 我用在其他动态语言的类包装上了,object<class_name>(Args ... args) 比如这样实例化一个 c#, js, 或者 java 类型 |
5 nightwitch 2021-11-24 09:24:57 +08:00 从此和 gcc 绑定了 |
9 sl0000 OP @ysc3839 你问:"你要实现什么?" 我答:“用来包装其他动态语言对象” 你问:“不管父类被什么类继承,父类中的函数都能拿到子类的名称?” 我答:“final” 你问:“所以动态语言调用以及 final 修饰和你给的这段代码有什么关系呢?” 我答:"js_object<js_class_name>(Args ... args) " |
10 ysc3839 2021-11-24 10:23:55 +08:00 via Android @sl0000 所以你是要获取某个类的名称字符串,然后传递给别的语言的引擎? 应该是没有符合 C++标准的方法的,但是允许使用编译器特性的话,可以看看 https://github.com/Neargye/nameof |
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. |
12 GeruzoniAnsasu 2021-11-24 12:58:10 +08:00 大概理解目的是想用模板「魔法」出一个 c++类,这个类用起来是 c++但实际上在动态语言里产生作用 那我只能说…… 给代码生产的迷惑性会远大于搞出来的那点点语法糖的甜度 比如你好不容易让类的名称可以自动绑定了、类本身看起来成员能动态增改了,对象也能(很扭曲地) -> 调用动态塞进去的新方法了 但不如构造函数里加个手动的 name 参数、手动访问 obj.members["fn"]、手动 vm->invoke(obj.members["fn"], ptr, args) |