Can I obtain C++ type names in a constexpr way?(我可以通过 constexpr 方式获取 C++ 类型名称吗?)
问题描述
我想在编译时使用类型的名称.例如,假设我写了:
I would like to use the name of a type at compile time. For example, suppose I've written:
现在我想要:
但是,唉,typeid(T).name()
只是 const char*
,而不是 constexpr...还有其他的,constexpr 方法来获取类型的名字?
But alas, typeid(T).name()
is just const char*
, not constexpr... is there some other, constexpr way to get a type's name?
推荐答案
基于此更新 回答 非constexpr 特定问题;是@HowardHinnant、@康桓玮@Val 和我本人等多人改进的结果.
Updated based on this answer to the non-constexpr-specific question; it is the result of refinements by several people including @HowardHinnant, @康桓瑋 @Val and myself.
据我所知,语言标准没有提供任何获取类型名称的工具.因此,我们求助于特定于编译器的方法.这适用于 GCC、clang 和 MSVC.
The language standard does not - to my knowledge - provide any facility for obtaining type names. So, we resort to compiler-specific approaches. This works with GCC, clang and MSVC.
这篇关于我可以通过 constexpr 方式获取 C++ 类型名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!