For example:
例如:
int a = 12;
cout << typeof(a) << endl;
Expected output:
预期的输出:
int
15 个解决方案
#1
312
C++11 update to a very old question: Print variable type in C++.
c++ 11更新了一个非常古老的问题:在c++中打印变量类型。
The accepted (and good) answer is to use typeid(a).name()
, where a
is a variable name.
可以接受的(也是好的)答案是使用typeid(a).name(),其中a是变量名。
Now in C++11 we have decltype(x)
, which can turn an expression into a type. And decltype()
comes with its own set of very interesting rules. For example decltype(a)
and decltype((a))
will generally be different types (and for good and understandable reasons once those reasons are exposed).
现在,在c++ 11中,我们有decltype(x),它可以将表达式转换为类型。decltype()有自己的一套非常有趣的规则。例如,decltype(a)和decltype(a)通常会是不同的类型(一旦这些理由被揭露出来,出于好的和可以理解的原因)。
Will our trusty typeid(a).name()
help us explore this brave new world?
我们值得信赖的类型(a).name()能帮助我们探索这个美丽的新世界吗?
No.
不。
But the tool that will is not that complicated. And it is that tool which I am using as an answer to this question. I will compare and contrast this new tool to typeid(a).name()
. And this new tool is actually built on top of typeid(a).name()
.
但是这个工具并不复杂。我用这个工具来回答这个问题。我将把这个新工具与typeid(a).name()进行比较和对比。这个新工具实际上构建在typeid(a).name()之上。
The fundamental issue:
基本问题:
typeid(a).name()
throws away cv-qualifiers, references, and lvalue/rvalue-ness. For example:
抛弃cv-限定符、引用和lvalue/rvalue-ness。例如:
const int ci = 0;
std::cout << typeid(ci).name() << '\n';
For me outputs:
对我来说输出:
i
and I'm guessing on MSVC outputs:
我猜MSVC的输出:
int
I.e. the const
is gone. This is not a QOI (Quality Of Implementation) issue. The standard mandates this behavior.
也就是说const消失了。这不是一个QOI(实现质量)问题。标准规定了这种行为。
What I'm recommending below is:
以下是我的建议:
template <typename T> std::string type_name();
which would be used like this:
可以这样使用:
const int ci = 0;
std::cout << type_name<decltype(ci)>() << '\n';
and for me outputs:
对我来说输出:
int const
<disclaimer>
I have not tested this on MSVC. </disclaimer>
But I welcome feedback from those who do.
<免责声明> 我还没有在MSVC上测试过。 ,但我欢迎那些做这些事情的人的反馈。
The C++11 Solution
c++ 11的解决方案
I am using __cxa_demangle
for non-MSVC platforms as recommend by ipapadop in his answer to demangle types. But on MSVC I'm trusting typeid
to demangle names (untested). And this core is wrapped around some simple testing that detects, restores and reports cv-qualifiers and references to the input type.
我使用了针对非msvc平台的__cxa_demangle作为对demangle类型的回答的ipapadop推荐。但是在MSVC中,我信任typeid来冒名(未经测试)。而这个核心是围绕着一些简单的测试,检测、恢复和报告cv-qualifiers和对输入类型的引用。
#include <type_traits>
#include <typeinfo>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>
template <class T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
The Results
结果
With this solution I can do this:
有了这个解决方案,我可以做到:
int& foo_lref();
int&& foo_rref();
int foo_value();
int
main()
{
int i = 0;
const int ci = 0;
std::cout << "decltype(i) is " << type_name<decltype(i)>() << '\n';
std::cout << "decltype((i)) is " << type_name<decltype((i))>() << '\n';
std::cout << "decltype(ci) is " << type_name<decltype(ci)>() << '\n';
std::cout << "decltype((ci)) is " << type_name<decltype((ci))>() << '\n';
std::cout << "decltype(static_cast<int&>(i)) is " << type_name<decltype(static_cast<int&>(i))>() << '\n';
std::cout << "decltype(static_cast<int&&>(i)) is " << type_name<decltype(static_cast<int&&>(i))>() << '\n';
std::cout << "decltype(static_cast<int>(i)) is " << type_name<decltype(static_cast<int>(i))>() << '\n';
std::cout << "decltype(foo_lref()) is " << type_name<decltype(foo_lref())>() << '\n';
std::cout << "decltype(foo_rref()) is " << type_name<decltype(foo_rref())>() << '\n';
std::cout << "decltype(foo_value()) is " << type_name<decltype(foo_value())>() << '\n';
}
and the output is:
和输出是:
decltype(i) is int
decltype((i)) is int&
decltype(ci) is int const
decltype((ci)) is int const&
decltype(static_cast<int&>(i)) is int&
decltype(static_cast<int&&>(i)) is int&&
decltype(static_cast<int>(i)) is int
decltype(foo_lref()) is int&
decltype(foo_rref()) is int&&
decltype(foo_value()) is int
Note (for example) the difference between decltype(i)
and decltype((i))
. The former is the type of the declaration of i
. The latter is the "type" of the expression i
. (expressions never have reference type, but as a convention decltype
represents lvalue expressions with lvalue references).
注意(例如)decltype(i)和decltype(i)之间的区别。前者是i的声明类型,后者是表达式i的“类型”(表达式从来没有引用类型,但作为惯例,decltype表示带有lvalue引用的lvalue表达式)。
Thus this tool is an excellent vehicle just to learn about decltype
, in addition to exploring and debugging your own code.
因此,除了探索和调试您自己的代码之外,这个工具还是学习decltype的好工具。
In contrast, if I were to build this just on typeid(a).name()
, without adding back lost cv-qualifiers or references, the output would be:
相比之下,如果我只在typeid(a).name()上构建它,而不添加丢失的cv-限定符或引用,那么输出将是:
decltype(i) is int
decltype((i)) is int
decltype(ci) is int
decltype((ci)) is int
decltype(static_cast<int&>(i)) is int
decltype(static_cast<int&&>(i)) is int
decltype(static_cast<int>(i)) is int
decltype(foo_lref()) is int
decltype(foo_rref()) is int
decltype(foo_value()) is int
I.e. Every reference and cv-qualifier is stripped off.
也就是说,每个引用和cv限定符都被删除。
C++14 Update
c++ 14更新
Just when you think you've got a solution to a problem nailed, someone always comes out of nowhere and shows you a much better way. :-)
当你认为你已经找到了解决问题的方法时,总会有人不知从哪里冒出来,告诉你一个更好的方法。:-)
This answer from Jamboree shows how to get the type name in C++14 at compile time. It is a brilliant solution for a couple reasons:
Jamboree的这个答案显示了如何在编译时用c++ 14获取类型名。这是一个绝妙的解决方案,原因如下:
- It's at compile time!
- 这是在编译时!
- You get the compiler itself to do the job instead of a library (even a std::lib). This means more accurate results for the latest language features (like lambdas).
- 你让编译器自己做这个工作,而不是一个库(甚至是std::lib)。这意味着对最新语言特性(如lambdas)有更精确的结果。
Jamboree's answer doesn't quite lay everything out for VS, and I'm tweaking his code a little bit. But since this answer gets a lot of views, take some time to go over there and upvote his answer, without which, this update would never have happened.
Jamboree的答案并没有把所有的东西都列出来,我正在修改他的代码。但是由于这个答案得到了很多的意见,所以需要花一些时间去那里,并为他的答案投赞成票,否则,这个更新将永远不会发生。
#include <cstddef>
#include <stdexcept>
#include <cstring>
#include <ostream>
#ifndef _MSC_VER
# if __cplusplus < 201103
# define CONSTEXPR11_TN
# define CONSTEXPR14_TN
# define NOEXCEPT_TN
# elif __cplusplus < 201402
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN
# define NOEXCEPT_TN noexcept
# else
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN constexpr
# define NOEXCEPT_TN noexcept
# endif
#else // _MSC_VER
# if _MSC_VER < 1900
# define CONSTEXPR11_TN
# define CONSTEXPR14_TN
# define NOEXCEPT_TN
# elif _MSC_VER < 2000
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN
# define NOEXCEPT_TN noexcept
# else
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN constexpr
# define NOEXCEPT_TN noexcept
# endif
#endif // _MSC_VER
class static_string
{
const char* const p_;
const std::size_t sz_;
public:
typedef const char* const_iterator;
template <std::size_t N>
CONSTEXPR11_TN static_string(const char(&a)[N]) NOEXCEPT_TN
: p_(a)
, sz_(N-1)
{}
CONSTEXPR11_TN static_string(const char* p, std::size_t N) NOEXCEPT_TN
: p_(p)
, sz_(N)
{}
CONSTEXPR11_TN const char* data() const NOEXCEPT_TN {return p_;}
CONSTEXPR11_TN std::size_t size() const NOEXCEPT_TN {return sz_;}
CONSTEXPR11_TN const_iterator begin() const NOEXCEPT_TN {return p_;}
CONSTEXPR11_TN const_iterator end() const NOEXCEPT_TN {return p_ + sz_;}
CONSTEXPR11_TN char operator[](std::size_t n) const
{
return n < sz_ ? p_[n] : throw std::out_of_range("static_string");
}
};
inline
std::ostream&
operator<<(std::ostream& os, static_string const& s)
{
return os.write(s.data(), s.size());
}
template <class T>
CONSTEXPR14_TN
static_string
type_name()
{
#ifdef __clang__
static_string p = __PRETTY_FUNCTION__;
return static_string(p.data() + 31, p.size() - 31 - 1);
#elif defined(__GNUC__)
static_string p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return static_string(p.data() + 36, p.size() - 36 - 1);
# else
return static_string(p.data() + 46, p.size() - 46 - 1);
# endif
#elif defined(_MSC_VER)
static_string p = __FUNCSIG__;
return static_string(p.data() + 38, p.size() - 38 - 7);
#endif
}
This code will auto-backoff on the constexpr
if you're still stuck in ancient C++11. And if you're painting on the cave wall with C++98/03, the noexcept
is sacrificed as well.
如果您仍然停留在古c++ 11中,此代码将在constexpr上自动回滚。如果你用c++ 98/03在洞壁上作画,除了牺牲外,没有别的。
C++17 Update
c++ 17更新
In the comments below Lyberta points out that the new std::string_view
can replace static_string
:
Lyberta在下面的评论中指出,新的std::string_view可以替代static_string:
template <class T>
constexpr
std::string_view
type_name()
{
using namespace std;
#ifdef __clang__
string_view p = __PRETTY_FUNCTION__;
return string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
string_view p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return string_view(p.data() + 36, p.size() - 36 - 1);
# else
return string_view(p.data() + 49, p.find(';', 49) - 49);
# endif
#elif defined(_MSC_VER)
string_view p = __FUNCSIG__;
return string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}
I've updated the constants for VS thanks to the very nice detective work by Jive Dadson in the comments below.
我已经更新了VS的常量,感谢Jive Dadson在下面的评论。
#2
209
Try:
试一试:
#include <typeinfo>
// …
std::cout << typeid(a).name() << '\n';
You might have to activate RTTI in your compiler options for this to work. Additionally, the output of this depends on the compiler. It might be a raw type name or a name mangling symbol or anything in between.
您可能需要在编译器选项中激活RTTI才能使其工作。此外,它的输出取决于编译器。它可能是原始类型名或名称管理符号或介于两者之间的任何东西。
#3
48
Very ugly but does the trick if you only want compile time info (e.g. for debugging):
非常难看,但是如果您只想要编译时信息(例如,用于调试),那么就需要这个技巧:
auto testVar = std::make_tuple(1, 1.0, "abc");
static_assert(decltype(testVar)::dummy_error, "DUMP MY TYPE" );
Returns:
返回:
Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:5:19: error: 'dummy_error' is not a member of 'std::tuple<int, double, const char*>'
#4
46
Don't forget to include <typeinfo>
不要忘记包括
I believe what you are referring to is runtime type identification. You can achieve the above by doing .
我相信您所说的是运行时类型标识。你可以通过这样做来达到上述目的。
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
int i;
cout << typeid(i).name();
return 0;
}
#5
19
Note that the names generated by the RTTI feature of C++ is not portable. For example, the class
注意,由c++的RTTI特性生成的名称是不可移植的。例如,类
MyNamespace::CMyContainer<int, test_MyNamespace::CMyObject>
will have the following names:
将会有以下名字:
// MSVC 2003:
class MyNamespace::CMyContainer[int,class test_MyNamespace::CMyObject]
// G++ 4.2:
N8MyNamespace8CMyContainerIiN13test_MyNamespace9CMyObjectEEE
So you can't use this information for serialization. But still, the typeid(a).name() property can still be used for log/debug purposes
所以你不能将这些信息用于序列化。但是,typeid(a).name()属性仍然可以用于日志/调试目的
#6
16
You can use templates.
您可以使用模板。
template <typename T> const char* typeof(T&) { return "unknown"; } // default
template<> const char* typeof(int&) { return "int"; }
template<> const char* typeof(float&) { return "float"; }
In the example above, when the type is not matched it will print "unknown".
在上面的例子中,当类型不匹配时,它将打印“未知”。
#7
12
As mentioned, typeid().name()
may return a mangled name. In GCC (and some other compilers) you can work around it with the following code:
如前所述,typeid().name()可能返回一个错误的名称。在GCC(以及其他一些编译器)中,您可以使用以下代码来解决它:
#include <cxxabi.h>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
namespace some_namespace { namespace another_namespace {
class my_class { };
} }
int main() {
typedef some_namespace::another_namespace::my_class my_type;
// mangled
std::cout << typeid(my_type).name() << std::endl;
// unmangled
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(my_type).name(), 0, 0, &status);
switch (status) {
case -1: {
// could not allocate memory
std::cout << "Could not allocate memory" << std::endl;
return -1;
} break;
case -2: {
// invalid name under the C++ ABI mangling rules
std::cout << "Invalid name" << std::endl;
return -1;
} break;
case -3: {
// invalid argument
std::cout << "Invalid argument to demangle()" << std::endl;
return -1;
} break;
}
std::cout << demangled << std::endl;
free(demangled);
return 0;
}
}
#8
9
You could use a traits class for this. Something like:
你可以用traits类来做这个。喜欢的东西:
#include <iostream>
using namespace std;
template <typename T> class type_name {
public:
static const char *name;
};
#define DECLARE_TYPE_NAME(x) template<> const char *type_name<x>::name = #x;
#define GET_TYPE_NAME(x) (type_name<typeof(x)>::name)
DECLARE_TYPE_NAME(int);
int main()
{
int a = 12;
cout << GET_TYPE_NAME(a) << endl;
}
The DECLARE_TYPE_NAME
define exists to make your life easier in declaring this traits class for all the types you expect to need.
DECLARE_TYPE_NAME定义可以使您更容易地为您期望的所有类型声明这个traits类。
This might be more useful than the solutions involving typeid
because you get to control the output. For example, using typeid
for long long
on my compiler gives "x".
这可能比涉及typeid的解决方案更有用,因为您可以控制输出。例如,在编译器上长时间使用typeid会得到“x”。
#9
4
The other answers involving RTTI (typeid) are probably what you want, as long as:
其他涉及RTTI (typeid)的答案可能就是你想要的,只要:
- you can afford the memory overhead (which can be considerable with some compilers)
- 您可以负担得起内存开销(对于某些编译器来说,内存开销相当大)
- the class names your compiler returns are useful
- 编译器返回的类名是有用的
The alternative, (similar to Greg Hewgill's answer), is to build a compile-time table of traits.
另一种选择(类似于Greg Hewgill的回答)是建立一个编译时的特性表。
template <typename T> struct type_as_string;
// declare your Wibble type (probably with definition of Wibble)
template <>
struct type_as_string<Wibble>
{
static const char* const value = "Wibble";
};
Be aware that if you wrap the declarations in a macro, you'll have trouble declaring names for template types taking more than one parameter (e.g. std::map), due to the comma.
请注意,如果您将声明封装在一个宏中,由于使用了逗号,您将难以声明使用多个参数(例如std:::map)的模板类型的名称。
To access the name of the type of a variable, all you need is
要访问变量类型的名称,您所需要的就是
template <typename T>
const char* get_type_as_string(const T&)
{
return type_as_string<T>::value;
}
#10
4
In C++11, we have decltype. There is no way in standard c++ to display exact type of variable declared using decltype. We can use boost typeindex i.e type_id_with_cvr
(cvr stands for const, volatile, reference) to print type like below.
在c++ 11中,我们有decltype。在标准c++中,不可能显示使用decltype声明的变量的确切类型。我们可以使用boost typeindex i。e type_id_with_cvr (cvr代表const、volatile、reference)以打印类型如下所示。
#include <iostream>
#include <boost/type_index.hpp>
using namespace std;
using boost::typeindex::type_id_with_cvr;
int main() {
int i = 0;
const int ci = 0;
cout << "decltype(i) is " << type_id_with_cvr<decltype(i)>().pretty_name() << '\n';
cout << "decltype((i)) is " << type_id_with_cvr<decltype((i))>().pretty_name() << '\n';
cout << "decltype(ci) is " << type_id_with_cvr<decltype(ci)>().pretty_name() << '\n';
cout << "decltype((ci)) is " << type_id_with_cvr<decltype((ci))>().pretty_name() << '\n';
cout << "decltype(std::move(i)) is " << type_id_with_cvr<decltype(std::move(i))>().pretty_name() << '\n';
cout << "decltype(std::static_cast<int&&>(i)) is " << type_id_with_cvr<decltype(static_cast<int&&>(i))>().pretty_name() << '\n';
return 0;
}
#11
3
A more generic solution without function overloading than my previous one:
一个比我之前的解决方案更通用的解决方案:
template<typename T>
std::string TypeOf(T){
std::string Type="unknown";
if(std::is_same<T,int>::value) Type="int";
if(std::is_same<T,std::string>::value) Type="String";
if(std::is_same<T,MyClass>::value) Type="MyClass";
return Type;}
Here MyClass is user defined class. More conditions can be added here as well.
这里MyClass是用户定义的类。这里还可以添加更多的条件。
Example:
例子:
#include <iostream>
class MyClass{};
template<typename T>
std::string TypeOf(T){
std::string Type="unknown";
if(std::is_same<T,int>::value) Type="int";
if(std::is_same<T,std::string>::value) Type="String";
if(std::is_same<T,MyClass>::value) Type="MyClass";
return Type;}
int main(){;
int a=0;
std::string s="";
MyClass my;
std::cout<<TypeOf(a)<<std::endl;
std::cout<<TypeOf(s)<<std::endl;
std::cout<<TypeOf(my)<<std::endl;
return 0;}
Output:
输出:
int
String
MyClass
#12
3
I like Nick's method, A complete form might be this (for all basic data types):
我喜欢Nick的方法,一个完整的表单可能是这样的(对于所有基本数据类型):
template <typename T> const char* typeof(T&) { return "unknown"; } // default
template<> const char* typeof(int&) { return "int"; }
template<> const char* typeof(short&) { return "short"; }
template<> const char* typeof(long&) { return "long"; }
template<> const char* typeof(unsigned&) { return "unsigned"; }
template<> const char* typeof(unsigned short&) { return "unsigned short"; }
template<> const char* typeof(unsigned long&) { return "unsigned long"; }
template<> const char* typeof(float&) { return "float"; }
template<> const char* typeof(double&) { return "double"; }
template<> const char* typeof(long double&) { return "long double"; }
template<> const char* typeof(std::string&) { return "String"; }
template<> const char* typeof(char&) { return "char"; }
template<> const char* typeof(signed char&) { return "signed char"; }
template<> const char* typeof(unsigned char&) { return "unsigned char"; }
template<> const char* typeof(char*&) { return "char*"; }
template<> const char* typeof(signed char*&) { return "signed char*"; }
template<> const char* typeof(unsigned char*&) { return "unsigned char*"; }
#13
3
You may also use c++filt with option -t (type) to demangle the type name:
你也可以使用c++filt选项-t(类型)来demangle类型名称:
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
int main() {
auto x = 1;
string my_type = typeid(x).name();
system(("echo " + my_type + " | c++filt -t").c_str());
return 0;
}
Tested on linux only.
只测试了在linux上。
#14
0
#include <iostream>
#include <typeinfo>
using namespace std;
#define show_type_name(_t) \
system(("echo " + string(typeid(_t).name()) + " | c++filt -t").c_str())
int main() {
auto a = {"one", "two", "three"};
cout << "Type of a: " << typeid(a).name() << endl;
cout << "Real type of a:\n";
show_type_name(a);
for (auto s : a) {
if (string(s) == "one") {
cout << "Type of s: " << typeid(s).name() << endl;
cout << "Real type of s:\n";
show_type_name(s);
}
cout << s << endl;
}
int i = 5;
cout << "Type of i: " << typeid(i).name() << endl;
cout << "Real type of i:\n";
show_type_name(i);
return 0;
}
Output:
输出:
Type of a: St16initializer_listIPKcE
Real type of a:
std::initializer_list<char const*>
Type of s: PKc
Real type of s:
char const*
one
two
three
Type of i: i
Real type of i:
int
#15
0
As I challenge I decided to test how far can one go with platform-independent (hopefully) template trickery.
当我挑战时,我决定测试一个独立于平台(希望如此)的模板技巧能走多远。
The names are assembled completely at compilation time. (Which means typeid(T).name()
couldn't be used, thus you have to explicitly provide names for non-compound types. Otherwise placeholders will be displayed instead.)
这些名称在编译时完全组装起来。(这意味着不能使用typeid(T).name(),因此必须显式地为非复合类型提供名称。否则将显示占位符。
Example usage:
使用示例:
TYPE_NAME(int)
TYPE_NAME(void)
// You probably should list all primitive types here.
TYPE_NAME(std::string)
int main()
{
// A simple case
std::cout << type_name<void(*)(int)> << '\n';
// -> `void (*)(int)`
// Ugly mess case
// Note that compiler removes cv-qualifiers from parameters and replaces arrays with pointers.
std::cout << type_name<void (std::string::*(int[3],const int, void (*)(std::string)))(volatile int*const*)> << '\n';
// -> `void (std::string::*(int *,int,void (*)(std::string)))(volatile int *const*)`
// A case with undefined types
// If a type wasn't TYPE_NAME'd, it's replaced by a placeholder, one of `class?`, `union?`, `enum?` or `??`.
std::cout << type_name<std::ostream (*)(int, short)> << '\n';
// -> `class? (*)(int,??)`
// With appropriate TYPE_NAME's, the output would be `std::string (*)(int,short)`.
}
Code:
代码:
#include <type_traits>
#include <utility>
static constexpr std::size_t max_str_lit_len = 256;
template <std::size_t I, std::size_t N> constexpr char sl_at(const char (&str)[N])
{
if constexpr(I < N)
return str[I];
else
return '\0';
}
constexpr std::size_t sl_len(const char *str)
{
for (std::size_t i = 0; i < max_str_lit_len; i++)
if (str[i] == '\0')
return i;
return 0;
}
template <char ...C> struct str_lit
{
static constexpr char value[] {C..., '\0'};
static constexpr int size = sl_len(value);
template <typename F, typename ...P> struct concat_impl {using type = typename concat_impl<F>::type::template concat_impl<P...>::type;};
template <char ...CC> struct concat_impl<str_lit<CC...>> {using type = str_lit<C..., CC...>;};
template <typename ...P> using concat = typename concat_impl<P...>::type;
};
template <typename, const char *> struct trim_str_lit_impl;
template <std::size_t ...I, const char *S> struct trim_str_lit_impl<std::index_sequence<I...>, S>
{
using type = str_lit<S[I]...>;
};
template <std::size_t N, const char *S> using trim_str_lit = typename trim_str_lit_impl<std::make_index_sequence<N>, S>::type;
#define STR_LIT(str) ::trim_str_lit<::sl_len(str), ::str_lit<STR_TO_VA(str)>::value>
#define STR_TO_VA(str) STR_TO_VA_16(str,0),STR_TO_VA_16(str,16),STR_TO_VA_16(str,32),STR_TO_VA_16(str,48)
#define STR_TO_VA_16(str,off) STR_TO_VA_4(str,0+off),STR_TO_VA_4(str,4+off),STR_TO_VA_4(str,8+off),STR_TO_VA_4(str,12+off)
#define STR_TO_VA_4(str,off) ::sl_at<off+0>(str),::sl_at<off+1>(str),::sl_at<off+2>(str),::sl_at<off+3>(str)
template <char ...C> constexpr str_lit<C...> make_str_lit(str_lit<C...>) {return {};}
template <std::size_t N> constexpr auto make_str_lit(const char (&str)[N])
{
return trim_str_lit<sl_len((const char (&)[N])str), str>{};
}
template <std::size_t A, std::size_t B> struct cexpr_pow {static constexpr std::size_t value = A * cexpr_pow<A,B-1>::value;};
template <std::size_t A> struct cexpr_pow<A,0> {static constexpr std::size_t value = 1;};
template <std::size_t N, std::size_t X, typename = std::make_index_sequence<X>> struct num_to_str_lit_impl;
template <std::size_t N, std::size_t X, std::size_t ...Seq> struct num_to_str_lit_impl<N, X, std::index_sequence<Seq...>>
{
static constexpr auto func()
{
if constexpr (N >= cexpr_pow<10,X>::value)
return num_to_str_lit_impl<N, X+1>::func();
else
return str_lit<(N / cexpr_pow<10,X-1-Seq>::value % 10 + '0')...>{};
}
};
template <std::size_t N> using num_to_str_lit = decltype(num_to_str_lit_impl<N,1>::func());
using spa = str_lit<' '>;
using lpa = str_lit<'('>;
using rpa = str_lit<')'>;
using lbr = str_lit<'['>;
using rbr = str_lit<']'>;
using ast = str_lit<'*'>;
using amp = str_lit<'&'>;
using con = str_lit<'c','o','n','s','t'>;
using vol = str_lit<'v','o','l','a','t','i','l','e'>;
using con_vol = con::concat<spa, vol>;
using nsp = str_lit<':',':'>;
using com = str_lit<','>;
using unk = str_lit<'?','?'>;
using c_cla = str_lit<'c','l','a','s','s','?'>;
using c_uni = str_lit<'u','n','i','o','n','?'>;
using c_enu = str_lit<'e','n','u','m','?'>;
template <typename T> inline constexpr bool ptr_or_ref = std::is_pointer_v<T> || std::is_reference_v<T> || std::is_member_pointer_v<T>;
template <typename T> inline constexpr bool func_or_arr = std::is_function_v<T> || std::is_array_v<T>;
template <typename T> struct primitive_type_name {using value = unk;};
template <typename T, typename = std::enable_if_t<std::is_class_v<T>>> using enable_if_class = T;
template <typename T, typename = std::enable_if_t<std::is_union_v<T>>> using enable_if_union = T;
template <typename T, typename = std::enable_if_t<std::is_enum_v <T>>> using enable_if_enum = T;
template <typename T> struct primitive_type_name<enable_if_class<T>> {using value = c_cla;};
template <typename T> struct primitive_type_name<enable_if_union<T>> {using value = c_uni;};
template <typename T> struct primitive_type_name<enable_if_enum <T>> {using value = c_enu;};
template <typename T> struct type_name_impl;
template <typename T> using type_name_lit = std::conditional_t<std::is_same_v<typename primitive_type_name<T>::value::template concat<spa>,
typename type_name_impl<T>::l::template concat<typename type_name_impl<T>::r>>,
typename primitive_type_name<T>::value,
typename type_name_impl<T>::l::template concat<typename type_name_impl<T>::r>>;
template <typename T> inline constexpr const char *type_name = type_name_lit<T>::value;
template <typename T, typename = std::enable_if_t<!std::is_const_v<T> && !std::is_volatile_v<T>>> using enable_if_no_cv = T;
template <typename T> struct type_name_impl
{
using l = typename primitive_type_name<T>::value::template concat<spa>;
using r = str_lit<>;
};
template <typename T> struct type_name_impl<const T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<con>,
con::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<volatile T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<vol>,
vol::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<const volatile T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<con_vol>,
con_vol::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<T *>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, ast>,
typename type_name_impl<T>::l::template concat< ast>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T &>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, amp>,
typename type_name_impl<T>::l::template concat< amp>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T &&>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, amp, amp>,
typename type_name_impl<T>::l::template concat< amp, amp>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T, typename C> struct type_name_impl<T C::*>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, type_name_lit<C>, nsp, ast>,
typename type_name_impl<T>::l::template concat< type_name_lit<C>, nsp, ast>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<enable_if_no_cv<T[]>>
{
using l = typename type_name_impl<T>::l;
using r = lbr::concat<rbr, typename type_name_impl<T>::r>;
};
template <typename T, std::size_t N> struct type_name_impl<enable_if_no_cv<T[N]>>
{
using l = typename type_name_impl<T>::l;
using r = lbr::concat<num_to_str_lit<N>, rbr, typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T()>
{
using l = typename type_name_impl<T>::l;
using r = lpa::concat<rpa, typename type_name_impl<T>::r>;
};
template <typename T, typename P1, typename ...P> struct type_name_impl<T(P1, P...)>
{
using l = typename type_name_impl<T>::l;
using r = lpa::concat<type_name_lit<P1>,
com::concat<type_name_lit<P>>..., rpa, typename type_name_impl<T>::r>;
};
#define TYPE_NAME(t) template <> struct primitive_type_name<t> {using value = STR_LIT(#t);};
#1
312
C++11 update to a very old question: Print variable type in C++.
c++ 11更新了一个非常古老的问题:在c++中打印变量类型。
The accepted (and good) answer is to use typeid(a).name()
, where a
is a variable name.
可以接受的(也是好的)答案是使用typeid(a).name(),其中a是变量名。
Now in C++11 we have decltype(x)
, which can turn an expression into a type. And decltype()
comes with its own set of very interesting rules. For example decltype(a)
and decltype((a))
will generally be different types (and for good and understandable reasons once those reasons are exposed).
现在,在c++ 11中,我们有decltype(x),它可以将表达式转换为类型。decltype()有自己的一套非常有趣的规则。例如,decltype(a)和decltype(a)通常会是不同的类型(一旦这些理由被揭露出来,出于好的和可以理解的原因)。
Will our trusty typeid(a).name()
help us explore this brave new world?
我们值得信赖的类型(a).name()能帮助我们探索这个美丽的新世界吗?
No.
不。
But the tool that will is not that complicated. And it is that tool which I am using as an answer to this question. I will compare and contrast this new tool to typeid(a).name()
. And this new tool is actually built on top of typeid(a).name()
.
但是这个工具并不复杂。我用这个工具来回答这个问题。我将把这个新工具与typeid(a).name()进行比较和对比。这个新工具实际上构建在typeid(a).name()之上。
The fundamental issue:
基本问题:
typeid(a).name()
throws away cv-qualifiers, references, and lvalue/rvalue-ness. For example:
抛弃cv-限定符、引用和lvalue/rvalue-ness。例如:
const int ci = 0;
std::cout << typeid(ci).name() << '\n';
For me outputs:
对我来说输出:
i
and I'm guessing on MSVC outputs:
我猜MSVC的输出:
int
I.e. the const
is gone. This is not a QOI (Quality Of Implementation) issue. The standard mandates this behavior.
也就是说const消失了。这不是一个QOI(实现质量)问题。标准规定了这种行为。
What I'm recommending below is:
以下是我的建议:
template <typename T> std::string type_name();
which would be used like this:
可以这样使用:
const int ci = 0;
std::cout << type_name<decltype(ci)>() << '\n';
and for me outputs:
对我来说输出:
int const
<disclaimer>
I have not tested this on MSVC. </disclaimer>
But I welcome feedback from those who do.
<免责声明> 我还没有在MSVC上测试过。 ,但我欢迎那些做这些事情的人的反馈。
The C++11 Solution
c++ 11的解决方案
I am using __cxa_demangle
for non-MSVC platforms as recommend by ipapadop in his answer to demangle types. But on MSVC I'm trusting typeid
to demangle names (untested). And this core is wrapped around some simple testing that detects, restores and reports cv-qualifiers and references to the input type.
我使用了针对非msvc平台的__cxa_demangle作为对demangle类型的回答的ipapadop推荐。但是在MSVC中,我信任typeid来冒名(未经测试)。而这个核心是围绕着一些简单的测试,检测、恢复和报告cv-qualifiers和对输入类型的引用。
#include <type_traits>
#include <typeinfo>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>
template <class T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
The Results
结果
With this solution I can do this:
有了这个解决方案,我可以做到:
int& foo_lref();
int&& foo_rref();
int foo_value();
int
main()
{
int i = 0;
const int ci = 0;
std::cout << "decltype(i) is " << type_name<decltype(i)>() << '\n';
std::cout << "decltype((i)) is " << type_name<decltype((i))>() << '\n';
std::cout << "decltype(ci) is " << type_name<decltype(ci)>() << '\n';
std::cout << "decltype((ci)) is " << type_name<decltype((ci))>() << '\n';
std::cout << "decltype(static_cast<int&>(i)) is " << type_name<decltype(static_cast<int&>(i))>() << '\n';
std::cout << "decltype(static_cast<int&&>(i)) is " << type_name<decltype(static_cast<int&&>(i))>() << '\n';
std::cout << "decltype(static_cast<int>(i)) is " << type_name<decltype(static_cast<int>(i))>() << '\n';
std::cout << "decltype(foo_lref()) is " << type_name<decltype(foo_lref())>() << '\n';
std::cout << "decltype(foo_rref()) is " << type_name<decltype(foo_rref())>() << '\n';
std::cout << "decltype(foo_value()) is " << type_name<decltype(foo_value())>() << '\n';
}
and the output is:
和输出是:
decltype(i) is int
decltype((i)) is int&
decltype(ci) is int const
decltype((ci)) is int const&
decltype(static_cast<int&>(i)) is int&
decltype(static_cast<int&&>(i)) is int&&
decltype(static_cast<int>(i)) is int
decltype(foo_lref()) is int&
decltype(foo_rref()) is int&&
decltype(foo_value()) is int
Note (for example) the difference between decltype(i)
and decltype((i))
. The former is the type of the declaration of i
. The latter is the "type" of the expression i
. (expressions never have reference type, but as a convention decltype
represents lvalue expressions with lvalue references).
注意(例如)decltype(i)和decltype(i)之间的区别。前者是i的声明类型,后者是表达式i的“类型”(表达式从来没有引用类型,但作为惯例,decltype表示带有lvalue引用的lvalue表达式)。
Thus this tool is an excellent vehicle just to learn about decltype
, in addition to exploring and debugging your own code.
因此,除了探索和调试您自己的代码之外,这个工具还是学习decltype的好工具。
In contrast, if I were to build this just on typeid(a).name()
, without adding back lost cv-qualifiers or references, the output would be:
相比之下,如果我只在typeid(a).name()上构建它,而不添加丢失的cv-限定符或引用,那么输出将是:
decltype(i) is int
decltype((i)) is int
decltype(ci) is int
decltype((ci)) is int
decltype(static_cast<int&>(i)) is int
decltype(static_cast<int&&>(i)) is int
decltype(static_cast<int>(i)) is int
decltype(foo_lref()) is int
decltype(foo_rref()) is int
decltype(foo_value()) is int
I.e. Every reference and cv-qualifier is stripped off.
也就是说,每个引用和cv限定符都被删除。
C++14 Update
c++ 14更新
Just when you think you've got a solution to a problem nailed, someone always comes out of nowhere and shows you a much better way. :-)
当你认为你已经找到了解决问题的方法时,总会有人不知从哪里冒出来,告诉你一个更好的方法。:-)
This answer from Jamboree shows how to get the type name in C++14 at compile time. It is a brilliant solution for a couple reasons:
Jamboree的这个答案显示了如何在编译时用c++ 14获取类型名。这是一个绝妙的解决方案,原因如下:
- It's at compile time!
- 这是在编译时!
- You get the compiler itself to do the job instead of a library (even a std::lib). This means more accurate results for the latest language features (like lambdas).
- 你让编译器自己做这个工作,而不是一个库(甚至是std::lib)。这意味着对最新语言特性(如lambdas)有更精确的结果。
Jamboree's answer doesn't quite lay everything out for VS, and I'm tweaking his code a little bit. But since this answer gets a lot of views, take some time to go over there and upvote his answer, without which, this update would never have happened.
Jamboree的答案并没有把所有的东西都列出来,我正在修改他的代码。但是由于这个答案得到了很多的意见,所以需要花一些时间去那里,并为他的答案投赞成票,否则,这个更新将永远不会发生。
#include <cstddef>
#include <stdexcept>
#include <cstring>
#include <ostream>
#ifndef _MSC_VER
# if __cplusplus < 201103
# define CONSTEXPR11_TN
# define CONSTEXPR14_TN
# define NOEXCEPT_TN
# elif __cplusplus < 201402
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN
# define NOEXCEPT_TN noexcept
# else
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN constexpr
# define NOEXCEPT_TN noexcept
# endif
#else // _MSC_VER
# if _MSC_VER < 1900
# define CONSTEXPR11_TN
# define CONSTEXPR14_TN
# define NOEXCEPT_TN
# elif _MSC_VER < 2000
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN
# define NOEXCEPT_TN noexcept
# else
# define CONSTEXPR11_TN constexpr
# define CONSTEXPR14_TN constexpr
# define NOEXCEPT_TN noexcept
# endif
#endif // _MSC_VER
class static_string
{
const char* const p_;
const std::size_t sz_;
public:
typedef const char* const_iterator;
template <std::size_t N>
CONSTEXPR11_TN static_string(const char(&a)[N]) NOEXCEPT_TN
: p_(a)
, sz_(N-1)
{}
CONSTEXPR11_TN static_string(const char* p, std::size_t N) NOEXCEPT_TN
: p_(p)
, sz_(N)
{}
CONSTEXPR11_TN const char* data() const NOEXCEPT_TN {return p_;}
CONSTEXPR11_TN std::size_t size() const NOEXCEPT_TN {return sz_;}
CONSTEXPR11_TN const_iterator begin() const NOEXCEPT_TN {return p_;}
CONSTEXPR11_TN const_iterator end() const NOEXCEPT_TN {return p_ + sz_;}
CONSTEXPR11_TN char operator[](std::size_t n) const
{
return n < sz_ ? p_[n] : throw std::out_of_range("static_string");
}
};
inline
std::ostream&
operator<<(std::ostream& os, static_string const& s)
{
return os.write(s.data(), s.size());
}
template <class T>
CONSTEXPR14_TN
static_string
type_name()
{
#ifdef __clang__
static_string p = __PRETTY_FUNCTION__;
return static_string(p.data() + 31, p.size() - 31 - 1);
#elif defined(__GNUC__)
static_string p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return static_string(p.data() + 36, p.size() - 36 - 1);
# else
return static_string(p.data() + 46, p.size() - 46 - 1);
# endif
#elif defined(_MSC_VER)
static_string p = __FUNCSIG__;
return static_string(p.data() + 38, p.size() - 38 - 7);
#endif
}
This code will auto-backoff on the constexpr
if you're still stuck in ancient C++11. And if you're painting on the cave wall with C++98/03, the noexcept
is sacrificed as well.
如果您仍然停留在古c++ 11中,此代码将在constexpr上自动回滚。如果你用c++ 98/03在洞壁上作画,除了牺牲外,没有别的。
C++17 Update
c++ 17更新
In the comments below Lyberta points out that the new std::string_view
can replace static_string
:
Lyberta在下面的评论中指出,新的std::string_view可以替代static_string:
template <class T>
constexpr
std::string_view
type_name()
{
using namespace std;
#ifdef __clang__
string_view p = __PRETTY_FUNCTION__;
return string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
string_view p = __PRETTY_FUNCTION__;
# if __cplusplus < 201402
return string_view(p.data() + 36, p.size() - 36 - 1);
# else
return string_view(p.data() + 49, p.find(';', 49) - 49);
# endif
#elif defined(_MSC_VER)
string_view p = __FUNCSIG__;
return string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}
I've updated the constants for VS thanks to the very nice detective work by Jive Dadson in the comments below.
我已经更新了VS的常量,感谢Jive Dadson在下面的评论。
#2
209
Try:
试一试:
#include <typeinfo>
// …
std::cout << typeid(a).name() << '\n';
You might have to activate RTTI in your compiler options for this to work. Additionally, the output of this depends on the compiler. It might be a raw type name or a name mangling symbol or anything in between.
您可能需要在编译器选项中激活RTTI才能使其工作。此外,它的输出取决于编译器。它可能是原始类型名或名称管理符号或介于两者之间的任何东西。
#3
48
Very ugly but does the trick if you only want compile time info (e.g. for debugging):
非常难看,但是如果您只想要编译时信息(例如,用于调试),那么就需要这个技巧:
auto testVar = std::make_tuple(1, 1.0, "abc");
static_assert(decltype(testVar)::dummy_error, "DUMP MY TYPE" );
Returns:
返回:
Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:5:19: error: 'dummy_error' is not a member of 'std::tuple<int, double, const char*>'
#4
46
Don't forget to include <typeinfo>
不要忘记包括
I believe what you are referring to is runtime type identification. You can achieve the above by doing .
我相信您所说的是运行时类型标识。你可以通过这样做来达到上述目的。
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
int i;
cout << typeid(i).name();
return 0;
}
#5
19
Note that the names generated by the RTTI feature of C++ is not portable. For example, the class
注意,由c++的RTTI特性生成的名称是不可移植的。例如,类
MyNamespace::CMyContainer<int, test_MyNamespace::CMyObject>
will have the following names:
将会有以下名字:
// MSVC 2003:
class MyNamespace::CMyContainer[int,class test_MyNamespace::CMyObject]
// G++ 4.2:
N8MyNamespace8CMyContainerIiN13test_MyNamespace9CMyObjectEEE
So you can't use this information for serialization. But still, the typeid(a).name() property can still be used for log/debug purposes
所以你不能将这些信息用于序列化。但是,typeid(a).name()属性仍然可以用于日志/调试目的
#6
16
You can use templates.
您可以使用模板。
template <typename T> const char* typeof(T&) { return "unknown"; } // default
template<> const char* typeof(int&) { return "int"; }
template<> const char* typeof(float&) { return "float"; }
In the example above, when the type is not matched it will print "unknown".
在上面的例子中,当类型不匹配时,它将打印“未知”。
#7
12
As mentioned, typeid().name()
may return a mangled name. In GCC (and some other compilers) you can work around it with the following code:
如前所述,typeid().name()可能返回一个错误的名称。在GCC(以及其他一些编译器)中,您可以使用以下代码来解决它:
#include <cxxabi.h>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
namespace some_namespace { namespace another_namespace {
class my_class { };
} }
int main() {
typedef some_namespace::another_namespace::my_class my_type;
// mangled
std::cout << typeid(my_type).name() << std::endl;
// unmangled
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(my_type).name(), 0, 0, &status);
switch (status) {
case -1: {
// could not allocate memory
std::cout << "Could not allocate memory" << std::endl;
return -1;
} break;
case -2: {
// invalid name under the C++ ABI mangling rules
std::cout << "Invalid name" << std::endl;
return -1;
} break;
case -3: {
// invalid argument
std::cout << "Invalid argument to demangle()" << std::endl;
return -1;
} break;
}
std::cout << demangled << std::endl;
free(demangled);
return 0;
}
}
#8
9
You could use a traits class for this. Something like:
你可以用traits类来做这个。喜欢的东西:
#include <iostream>
using namespace std;
template <typename T> class type_name {
public:
static const char *name;
};
#define DECLARE_TYPE_NAME(x) template<> const char *type_name<x>::name = #x;
#define GET_TYPE_NAME(x) (type_name<typeof(x)>::name)
DECLARE_TYPE_NAME(int);
int main()
{
int a = 12;
cout << GET_TYPE_NAME(a) << endl;
}
The DECLARE_TYPE_NAME
define exists to make your life easier in declaring this traits class for all the types you expect to need.
DECLARE_TYPE_NAME定义可以使您更容易地为您期望的所有类型声明这个traits类。
This might be more useful than the solutions involving typeid
because you get to control the output. For example, using typeid
for long long
on my compiler gives "x".
这可能比涉及typeid的解决方案更有用,因为您可以控制输出。例如,在编译器上长时间使用typeid会得到“x”。
#9
4
The other answers involving RTTI (typeid) are probably what you want, as long as:
其他涉及RTTI (typeid)的答案可能就是你想要的,只要:
- you can afford the memory overhead (which can be considerable with some compilers)
- 您可以负担得起内存开销(对于某些编译器来说,内存开销相当大)
- the class names your compiler returns are useful
- 编译器返回的类名是有用的
The alternative, (similar to Greg Hewgill's answer), is to build a compile-time table of traits.
另一种选择(类似于Greg Hewgill的回答)是建立一个编译时的特性表。
template <typename T> struct type_as_string;
// declare your Wibble type (probably with definition of Wibble)
template <>
struct type_as_string<Wibble>
{
static const char* const value = "Wibble";
};
Be aware that if you wrap the declarations in a macro, you'll have trouble declaring names for template types taking more than one parameter (e.g. std::map), due to the comma.
请注意,如果您将声明封装在一个宏中,由于使用了逗号,您将难以声明使用多个参数(例如std:::map)的模板类型的名称。
To access the name of the type of a variable, all you need is
要访问变量类型的名称,您所需要的就是
template <typename T>
const char* get_type_as_string(const T&)
{
return type_as_string<T>::value;
}
#10
4
In C++11, we have decltype. There is no way in standard c++ to display exact type of variable declared using decltype. We can use boost typeindex i.e type_id_with_cvr
(cvr stands for const, volatile, reference) to print type like below.
在c++ 11中,我们有decltype。在标准c++中,不可能显示使用decltype声明的变量的确切类型。我们可以使用boost typeindex i。e type_id_with_cvr (cvr代表const、volatile、reference)以打印类型如下所示。
#include <iostream>
#include <boost/type_index.hpp>
using namespace std;
using boost::typeindex::type_id_with_cvr;
int main() {
int i = 0;
const int ci = 0;
cout << "decltype(i) is " << type_id_with_cvr<decltype(i)>().pretty_name() << '\n';
cout << "decltype((i)) is " << type_id_with_cvr<decltype((i))>().pretty_name() << '\n';
cout << "decltype(ci) is " << type_id_with_cvr<decltype(ci)>().pretty_name() << '\n';
cout << "decltype((ci)) is " << type_id_with_cvr<decltype((ci))>().pretty_name() << '\n';
cout << "decltype(std::move(i)) is " << type_id_with_cvr<decltype(std::move(i))>().pretty_name() << '\n';
cout << "decltype(std::static_cast<int&&>(i)) is " << type_id_with_cvr<decltype(static_cast<int&&>(i))>().pretty_name() << '\n';
return 0;
}
#11
3
A more generic solution without function overloading than my previous one:
一个比我之前的解决方案更通用的解决方案:
template<typename T>
std::string TypeOf(T){
std::string Type="unknown";
if(std::is_same<T,int>::value) Type="int";
if(std::is_same<T,std::string>::value) Type="String";
if(std::is_same<T,MyClass>::value) Type="MyClass";
return Type;}
Here MyClass is user defined class. More conditions can be added here as well.
这里MyClass是用户定义的类。这里还可以添加更多的条件。
Example:
例子:
#include <iostream>
class MyClass{};
template<typename T>
std::string TypeOf(T){
std::string Type="unknown";
if(std::is_same<T,int>::value) Type="int";
if(std::is_same<T,std::string>::value) Type="String";
if(std::is_same<T,MyClass>::value) Type="MyClass";
return Type;}
int main(){;
int a=0;
std::string s="";
MyClass my;
std::cout<<TypeOf(a)<<std::endl;
std::cout<<TypeOf(s)<<std::endl;
std::cout<<TypeOf(my)<<std::endl;
return 0;}
Output:
输出:
int
String
MyClass
#12
3
I like Nick's method, A complete form might be this (for all basic data types):
我喜欢Nick的方法,一个完整的表单可能是这样的(对于所有基本数据类型):
template <typename T> const char* typeof(T&) { return "unknown"; } // default
template<> const char* typeof(int&) { return "int"; }
template<> const char* typeof(short&) { return "short"; }
template<> const char* typeof(long&) { return "long"; }
template<> const char* typeof(unsigned&) { return "unsigned"; }
template<> const char* typeof(unsigned short&) { return "unsigned short"; }
template<> const char* typeof(unsigned long&) { return "unsigned long"; }
template<> const char* typeof(float&) { return "float"; }
template<> const char* typeof(double&) { return "double"; }
template<> const char* typeof(long double&) { return "long double"; }
template<> const char* typeof(std::string&) { return "String"; }
template<> const char* typeof(char&) { return "char"; }
template<> const char* typeof(signed char&) { return "signed char"; }
template<> const char* typeof(unsigned char&) { return "unsigned char"; }
template<> const char* typeof(char*&) { return "char*"; }
template<> const char* typeof(signed char*&) { return "signed char*"; }
template<> const char* typeof(unsigned char*&) { return "unsigned char*"; }
#13
3
You may also use c++filt with option -t (type) to demangle the type name:
你也可以使用c++filt选项-t(类型)来demangle类型名称:
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
int main() {
auto x = 1;
string my_type = typeid(x).name();
system(("echo " + my_type + " | c++filt -t").c_str());
return 0;
}
Tested on linux only.
只测试了在linux上。
#14
0
#include <iostream>
#include <typeinfo>
using namespace std;
#define show_type_name(_t) \
system(("echo " + string(typeid(_t).name()) + " | c++filt -t").c_str())
int main() {
auto a = {"one", "two", "three"};
cout << "Type of a: " << typeid(a).name() << endl;
cout << "Real type of a:\n";
show_type_name(a);
for (auto s : a) {
if (string(s) == "one") {
cout << "Type of s: " << typeid(s).name() << endl;
cout << "Real type of s:\n";
show_type_name(s);
}
cout << s << endl;
}
int i = 5;
cout << "Type of i: " << typeid(i).name() << endl;
cout << "Real type of i:\n";
show_type_name(i);
return 0;
}
Output:
输出:
Type of a: St16initializer_listIPKcE
Real type of a:
std::initializer_list<char const*>
Type of s: PKc
Real type of s:
char const*
one
two
three
Type of i: i
Real type of i:
int
#15
0
As I challenge I decided to test how far can one go with platform-independent (hopefully) template trickery.
当我挑战时,我决定测试一个独立于平台(希望如此)的模板技巧能走多远。
The names are assembled completely at compilation time. (Which means typeid(T).name()
couldn't be used, thus you have to explicitly provide names for non-compound types. Otherwise placeholders will be displayed instead.)
这些名称在编译时完全组装起来。(这意味着不能使用typeid(T).name(),因此必须显式地为非复合类型提供名称。否则将显示占位符。
Example usage:
使用示例:
TYPE_NAME(int)
TYPE_NAME(void)
// You probably should list all primitive types here.
TYPE_NAME(std::string)
int main()
{
// A simple case
std::cout << type_name<void(*)(int)> << '\n';
// -> `void (*)(int)`
// Ugly mess case
// Note that compiler removes cv-qualifiers from parameters and replaces arrays with pointers.
std::cout << type_name<void (std::string::*(int[3],const int, void (*)(std::string)))(volatile int*const*)> << '\n';
// -> `void (std::string::*(int *,int,void (*)(std::string)))(volatile int *const*)`
// A case with undefined types
// If a type wasn't TYPE_NAME'd, it's replaced by a placeholder, one of `class?`, `union?`, `enum?` or `??`.
std::cout << type_name<std::ostream (*)(int, short)> << '\n';
// -> `class? (*)(int,??)`
// With appropriate TYPE_NAME's, the output would be `std::string (*)(int,short)`.
}
Code:
代码:
#include <type_traits>
#include <utility>
static constexpr std::size_t max_str_lit_len = 256;
template <std::size_t I, std::size_t N> constexpr char sl_at(const char (&str)[N])
{
if constexpr(I < N)
return str[I];
else
return '\0';
}
constexpr std::size_t sl_len(const char *str)
{
for (std::size_t i = 0; i < max_str_lit_len; i++)
if (str[i] == '\0')
return i;
return 0;
}
template <char ...C> struct str_lit
{
static constexpr char value[] {C..., '\0'};
static constexpr int size = sl_len(value);
template <typename F, typename ...P> struct concat_impl {using type = typename concat_impl<F>::type::template concat_impl<P...>::type;};
template <char ...CC> struct concat_impl<str_lit<CC...>> {using type = str_lit<C..., CC...>;};
template <typename ...P> using concat = typename concat_impl<P...>::type;
};
template <typename, const char *> struct trim_str_lit_impl;
template <std::size_t ...I, const char *S> struct trim_str_lit_impl<std::index_sequence<I...>, S>
{
using type = str_lit<S[I]...>;
};
template <std::size_t N, const char *S> using trim_str_lit = typename trim_str_lit_impl<std::make_index_sequence<N>, S>::type;
#define STR_LIT(str) ::trim_str_lit<::sl_len(str), ::str_lit<STR_TO_VA(str)>::value>
#define STR_TO_VA(str) STR_TO_VA_16(str,0),STR_TO_VA_16(str,16),STR_TO_VA_16(str,32),STR_TO_VA_16(str,48)
#define STR_TO_VA_16(str,off) STR_TO_VA_4(str,0+off),STR_TO_VA_4(str,4+off),STR_TO_VA_4(str,8+off),STR_TO_VA_4(str,12+off)
#define STR_TO_VA_4(str,off) ::sl_at<off+0>(str),::sl_at<off+1>(str),::sl_at<off+2>(str),::sl_at<off+3>(str)
template <char ...C> constexpr str_lit<C...> make_str_lit(str_lit<C...>) {return {};}
template <std::size_t N> constexpr auto make_str_lit(const char (&str)[N])
{
return trim_str_lit<sl_len((const char (&)[N])str), str>{};
}
template <std::size_t A, std::size_t B> struct cexpr_pow {static constexpr std::size_t value = A * cexpr_pow<A,B-1>::value;};
template <std::size_t A> struct cexpr_pow<A,0> {static constexpr std::size_t value = 1;};
template <std::size_t N, std::size_t X, typename = std::make_index_sequence<X>> struct num_to_str_lit_impl;
template <std::size_t N, std::size_t X, std::size_t ...Seq> struct num_to_str_lit_impl<N, X, std::index_sequence<Seq...>>
{
static constexpr auto func()
{
if constexpr (N >= cexpr_pow<10,X>::value)
return num_to_str_lit_impl<N, X+1>::func();
else
return str_lit<(N / cexpr_pow<10,X-1-Seq>::value % 10 + '0')...>{};
}
};
template <std::size_t N> using num_to_str_lit = decltype(num_to_str_lit_impl<N,1>::func());
using spa = str_lit<' '>;
using lpa = str_lit<'('>;
using rpa = str_lit<')'>;
using lbr = str_lit<'['>;
using rbr = str_lit<']'>;
using ast = str_lit<'*'>;
using amp = str_lit<'&'>;
using con = str_lit<'c','o','n','s','t'>;
using vol = str_lit<'v','o','l','a','t','i','l','e'>;
using con_vol = con::concat<spa, vol>;
using nsp = str_lit<':',':'>;
using com = str_lit<','>;
using unk = str_lit<'?','?'>;
using c_cla = str_lit<'c','l','a','s','s','?'>;
using c_uni = str_lit<'u','n','i','o','n','?'>;
using c_enu = str_lit<'e','n','u','m','?'>;
template <typename T> inline constexpr bool ptr_or_ref = std::is_pointer_v<T> || std::is_reference_v<T> || std::is_member_pointer_v<T>;
template <typename T> inline constexpr bool func_or_arr = std::is_function_v<T> || std::is_array_v<T>;
template <typename T> struct primitive_type_name {using value = unk;};
template <typename T, typename = std::enable_if_t<std::is_class_v<T>>> using enable_if_class = T;
template <typename T, typename = std::enable_if_t<std::is_union_v<T>>> using enable_if_union = T;
template <typename T, typename = std::enable_if_t<std::is_enum_v <T>>> using enable_if_enum = T;
template <typename T> struct primitive_type_name<enable_if_class<T>> {using value = c_cla;};
template <typename T> struct primitive_type_name<enable_if_union<T>> {using value = c_uni;};
template <typename T> struct primitive_type_name<enable_if_enum <T>> {using value = c_enu;};
template <typename T> struct type_name_impl;
template <typename T> using type_name_lit = std::conditional_t<std::is_same_v<typename primitive_type_name<T>::value::template concat<spa>,
typename type_name_impl<T>::l::template concat<typename type_name_impl<T>::r>>,
typename primitive_type_name<T>::value,
typename type_name_impl<T>::l::template concat<typename type_name_impl<T>::r>>;
template <typename T> inline constexpr const char *type_name = type_name_lit<T>::value;
template <typename T, typename = std::enable_if_t<!std::is_const_v<T> && !std::is_volatile_v<T>>> using enable_if_no_cv = T;
template <typename T> struct type_name_impl
{
using l = typename primitive_type_name<T>::value::template concat<spa>;
using r = str_lit<>;
};
template <typename T> struct type_name_impl<const T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<con>,
con::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<volatile T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<vol>,
vol::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<const volatile T>
{
using new_T_l = std::conditional_t<type_name_impl<T>::l::size && !ptr_or_ref<T>,
spa::concat<typename type_name_impl<T>::l>,
typename type_name_impl<T>::l>;
using l = std::conditional_t<ptr_or_ref<T>,
typename new_T_l::template concat<con_vol>,
con_vol::concat<new_T_l>>;
using r = typename type_name_impl<T>::r;
};
template <typename T> struct type_name_impl<T *>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, ast>,
typename type_name_impl<T>::l::template concat< ast>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T &>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, amp>,
typename type_name_impl<T>::l::template concat< amp>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T &&>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, amp, amp>,
typename type_name_impl<T>::l::template concat< amp, amp>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T, typename C> struct type_name_impl<T C::*>
{
using l = std::conditional_t<func_or_arr<T>,
typename type_name_impl<T>::l::template concat<lpa, type_name_lit<C>, nsp, ast>,
typename type_name_impl<T>::l::template concat< type_name_lit<C>, nsp, ast>>;
using r = std::conditional_t<func_or_arr<T>,
rpa::concat<typename type_name_impl<T>::r>,
typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<enable_if_no_cv<T[]>>
{
using l = typename type_name_impl<T>::l;
using r = lbr::concat<rbr, typename type_name_impl<T>::r>;
};
template <typename T, std::size_t N> struct type_name_impl<enable_if_no_cv<T[N]>>
{
using l = typename type_name_impl<T>::l;
using r = lbr::concat<num_to_str_lit<N>, rbr, typename type_name_impl<T>::r>;
};
template <typename T> struct type_name_impl<T()>
{
using l = typename type_name_impl<T>::l;
using r = lpa::concat<rpa, typename type_name_impl<T>::r>;
};
template <typename T, typename P1, typename ...P> struct type_name_impl<T(P1, P...)>
{
using l = typename type_name_impl<T>::l;
using r = lpa::concat<type_name_lit<P1>,
com::concat<type_name_lit<P>>..., rpa, typename type_name_impl<T>::r>;
};
#define TYPE_NAME(t) template <> struct primitive_type_name<t> {using value = STR_LIT(#t);};