c++ - 窄播有什么作用?

标签 c++ guideline-support-library

我看到一个使用 narrow_cast 的代码像这样

int num = narrow_cast<int>(26.72);
cout << num;

问题是我的编译器说:
'narrow_cast' was not decleared in this scope. 

我应该定义 narrow_cast我自己还是我用错了方法还是没有像 narrow_cast 这样的东西?

最佳答案

narrow_cast gsl真的是static_cast .但它更明确,您可以稍后搜索它。您可以查看 implementation你自己:

// narrow_cast(): a searchable way to do narrowing casts of values
template <class T, class U>
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
constexpr T narrow_cast(U&& u) noexcept
{
    return static_cast<T>(std::forward<U>(u));
}
narrow_cast不是标准 C++ 的一部分。您需要gsl编译并运行它。您可能错过了这一点,这就是它没有编译的原因。

关于c++ - 窄播有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58615926/

相关文章:

c++ - 我应该如何表示我拥有的连续元素序列?

c++ - 如何在多个项目(以及一个版本的 cpplint.py)中使用单个 makefile?

c++ - MSSQL ODBC 拒绝有效的 TINYINT 值(数值超出范围)

c++ - 从字符串中制作一个字符的 Sprite

c++ - 将 SID 转换为字符串

c++ - 枚举值作为 gsl::multi_span 的索引

string-view - gsl::string_span 和 std::string_view 有何不同?

c++ - 使用 WSE 时无法从 C++ 客户端访问 Web 服务

c++ - 使用 cmake 将依赖项传播到仅 header 的 ExternalProject

c++ - 是否有可以与 GCC 4.9.x 一起使用的 GSL 实现?