为什么std :: isgraph在std :: use_facet中抛出异常?

时间:2019-01-18 23:27:38

标签: c++ character-encoding locale

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==46等于_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结构在调试器中看起来很合理:

enter image description here

为什么isgraph会引发异常?

0 个答案:

没有答案