我可以通过 constexpr 方式获取 C++ 类型名称吗?

Can I obtain C++ type names in a constexpr way?(我可以通过 constexpr 方式获取 C++ 类型名称吗?)

本文介绍了我可以通过 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++ 类型名称吗?

基础教程推荐