首页 > 解决方案 > 为什么 std::isgraph 在 std::use_facet 中抛出异常?

问题描述

bool foo (char32_t code_point)
{
    static std::locale loc ("en_US.utf8");
    return std::isgraph (code_point, loc);
}

foo ('B');

这会引发异常:

  /**
   *  @brief  Return a facet.
   *  @ingroup locales
   *
   *  use_facet looks for and returns a reference to a facet of type Facet
   *  where Facet is the template parameter.  If has_facet(locale) is true,
   *  there is a suitable facet to return.  It throws std::bad_cast if the
   *  locale doesn't contain a facet of type Facet.
   *
   *  @tparam  _Facet  The facet type to access.
   *  @param  __loc  The locale to use.
   *  @return  Reference to facet of type Facet.
   *  @throw  std::bad_cast if @p __loc doesn't contain a facet of type _Facet.
  */
  template<typename _Facet>
    const _Facet&
    use_facet(const locale& __loc)
    {
      const size_t __i = _Facet::id._M_id();
      const locale::facet** __facets = __loc._M_impl->_M_facets;
      if (__i >= __loc._M_impl->_M_facets_size || !__facets[__i])
        __throw_bad_cast();
#if __cpp_rtti
      return dynamic_cast<const _Facet&>(*__facets[__i]);
#else
      return static_cast<const _Facet&>(*__facets[__i]);
#endif
    }

它调用__throw_bad_cast(),因为__i==46which equals _M_facets_size

注意:文档确实说It throws std::bad_cast if the locale doesn't contain a facet of type Facet.

但我不知道那是什么意思。

在我的 Ubuntu 上,locale -a包括en_US.utf8,并且loc在调试器中结构看起来很合理:

在此处输入图像描述

为什么会isgraph抛出异常?

标签: c++character-encodinglocale

解决方案


推荐阅读