我已经知道,无论是在 C 还是 C++ 中,都不可能打印变量的名称,这在其他 StackOverflow 帖子中提到过。
但是我知道在其他语言中,比如 Delphi,可以打印对象的类名。我不知道这在 C++ 中是否可行。
因此我的问题:是否可以打印对象的对象名?
一些背景:我正在编写一个程序,它使用 STL 向量来跟踪位置和值(两者都在单独的向量中),并且我想打印向量的内容,前面是对象名称(以便我可以看到我是在查看位置还是在查看值)。
您可以使用:
typeid(someObject).name();
但是返回值是实现定义的,根本不需要返回任何有用或可读的内容。
返回实现定义的以 null 结尾的字符串 包含类型的名称。不提供任何保证, 特别是,对于多种类型,返回的字符串可以是相同的 在同一程序的调用之间进行更改。
因此,如果您不仅需要记录日志,那么我建议您自己跟踪它们。
.name()
.
这就是我用的:
#include <cstddef>
#include <cstring>
#include <iostream>
#include <ostream>
template < class T >
std::string type_name()
{
#ifdef __clang__
std::string p = __PRETTY_FUNCTION__;
return p.substr( 43, p.length() - 43 - 1 );
#elif defined( __GNUC__ )
std::string p = __PRETTY_FUNCTION__;
#if __cplusplus < 201402
return p.substr( 57, p.length() - 53 - 62 );
#else
return p.substr( 46, p.length() - 46 - 1 );
#endif
#elif defined( _MSC_VER )
std::string p = __FUNCSIG__;
return p.substr( 38, p.length() - 38 - 7 );
#else
return std::string("This function is not supported!");
#endif
}
它通常会提供人类友好的类型版本。根据您的编译器(和版本),子字符串可能会有所不同。
Coliru 示例:http://coliru.stacked-crooked.com/a/bcdb77a7519136ea
你可以使用
#include <typeinfo>
typeid(object).name()
这是相同的工作代码 -
#include <iostream>
#include <typeinfo>
using namespace std;
class Adi {
int a;
int b;
};
int main() {
Adi obj, obj2;
cout << typeid(obj2).name() << endl;
return 0;
}
输出: 3阿迪
输出格式为类名长度后跟其名称。
可以使用预处理器打印变量的名称:
#define STRINGIFY(a) #a
#define VAR_NAME(a) STRINGIFY( a)
int my_var = 42;
std::cout << "var name = " << VAR_NAME( my_var) << std::endl;
要获取类的名称,您可以使用模板类的重载,然后您只需为要获取名称的每种类型实现该类的部分特化即可。
template< typename T> class type
{
public:
static constexpr const char* name() {
return "unknown";
} // end type< T>::name
}; // type< T>
template<> class type< int>
{
public:
static constexpr const char* name() {
return "int";
}
}
template<> class type< std::string>
{
public:
static constexpr const char* name() {
return "std::string";
}
}
std::cout << "type name = " << type< int>::name() << std::endl;
所有 POD 类型和 STL 容器的此类和专业化可在此处获得: https://github.com/Gemini67/Celma
还包含获取变量类型名称的解决方案。