微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

确定 wchar_t 是内置类型还是别名

如何解决确定 wchar_t 是内置类型还是别名

是的,wchar_t 应该是 C++ 中的内置类型;不幸的是,某些(非常)旧的编译器并非如此。 ?

是否有(特定于编译器的,例如 GCC/g++)方法来确定 wchar_t 是内置类型(关键字)还是 typedef?原因是判断f(wchar_t)是否可以是重载;如果 wchar_ttypedef,则它(很可能)与 f(uint16_t)f(uint32_t) 相同。

Microsoft Visual C++ 有 _NATIVE_WCHAR_T_DEFINED 所以我可以写:

#include <stdint.h>

#if defined(_MSC_VER)
   #define MY_PROJECT_wchar_t_is_built_in_ (_NATIVE_WCHAR_T_DEFINED == 1)
#else
   #define MY_PROJECT_wchar_t_is_built_in_ 1
#endif

void f(uint16_t ch_utf16) { /* UTF-16 processing */ }
void f(uint32_t ch_utf32) { /* UTF-32 processing */ }

#if MY_PROJECT_wchar_t_is_built_in_
   #include <type_traits>
   void f(whcar_t ch_)
   {
      using wchar_t_type = std::conditional<sizeof(wchar_t) == sizeof(uint32_t),uint32_t,uint16_t>::type;
      #ifdef _WIN32
      static_assert(sizeof(wchar_t_type) == sizeof(uint16_t),"wchar_t should be 16-bits on Windows.");
      #endif
      const auto ch = reinterpret_cast<wchar_t_type>(ch_);
      f(ch);
   }
#endif

广泛的 TMP 可能不起作用(例如,std::enable_if)......因为它是一个旧的编译器,首先导致了问题!

解决方法

Ayxan Haqverdilisuggestion 在使用 templatesizeof() 的评论中解决了我的问题。 (这也说明了为什么关于“为什么?”的足够上下文是必要的。)

我的代码现在是:

void f_(uint16_t ch_utf16) { /* UTF-16 processing */ }
void f_(uint32_t ch_utf32) { /* UTF-32 processing */ }

template<typename T>
void f(T ch_)
{
    static_assert(sizeof(T) == sizeof(wchar_t),"T should be wchar_t");

    if (sizeof(T) == sizeof(uint16_t))
    {
        const auto ch = reinterpret_cast<uint16_t>(ch_);
        f_(ch);
    }
    else if (sizeof(T) == sizeof(uint32_t))
    {
        const auto ch = reinterpret_cast<uint32_t>(ch_);
        f_(ch);
    }
    else
    {
        throw ...;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。