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:
constexpr size_t my_strlen(const char* s)
{
const char* cp = s;
while(*cp != ' ') { cp++; };
return cp - s;
}
现在我想要:
template <typename T>
constexpr auto type_name_length = my_strlen(typeid(T).name());
但是,唉,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.
#include <string_view>
// If you can't use C++17's standard library, you'll need to use the GSL
// string_view or implement your own struct (which would not be very difficult,
// since we only need a few methods here)
template <typename T> constexpr std::string_view type_name();
template <>
constexpr std::string_view type_name<void>()
{ return "void"; }
namespace detail {
using type_name_prober = void;
template <typename T>
constexpr std::string_view wrapped_type_name()
{
#ifdef __clang__
return __PRETTY_FUNCTION__;
#elif defined(__GNUC__)
return __PRETTY_FUNCTION__;
#elif defined(_MSC_VER)
return __FUNCSIG__;
#else
#error "Unsupported compiler"
#endif
}
constexpr std::size_t wrapped_type_name_prefix_length() {
return wrapped_type_name<type_name_prober>().find(type_name<type_name_prober>());
}
constexpr std::size_t wrapped_type_name_suffix_length() {
return wrapped_type_name<type_name_prober>().length()
- wrapped_type_name_prefix_length()
- type_name<type_name_prober>().length();
}
} // namespace detail
template <typename T>
constexpr std::string_view type_name() {
constexpr auto wrapped_name = detail::wrapped_type_name<T>();
constexpr auto prefix_length = detail::wrapped_type_name_prefix_length();
constexpr auto suffix_length = detail::wrapped_type_name_suffix_length();
constexpr auto type_name_length = wrapped_name.length() - prefix_length - suffix_length;
return wrapped_name.substr(prefix_length, type_name_length);
}
这篇关于我可以通过 constexpr 方式获取 C++ 类型名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以通过 constexpr 方式获取 C++ 类型名称吗?
基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
