c++ - 为什么 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==46
which 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
抛出异常?
解决方案
推荐阅读
- sql - 在 SQL 中多次旋转同一列
- javascript - 如何从 Javascript 中的对象列表中记录一组值?
- java - 只是做了一些练习,遇到了一些问题,不知道是逻辑还是什么。我需要一些眼睛
- git - 如何将 Github 中的分支克隆到 Google Cloud Source Repository 中的新分支?
- reactjs - React Native - 缓存图像
- javascript - 如何使用 ionic v4 项目中的代码删除 android 应用程序缓存
- sql-server - SELECT 失败,因为以下 SET 选项的设置不正确:“ANSI_WARNINGS”
- c++ - 如何在 C++ 中创建 OpenVPN 客户端?(不是一个 tun/tap 经理,一个真正的客户)
- python - 在没有 pip 的情况下安装 matplotlib 和 numpy
- python-3.x - 将变量重要性图导出为图像