我看到一个使用 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/