C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了const_forward在C中的可选实现中做了什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨我在 here看到std :: optional实现之一,我发现这段代码让我感到困惑:

// workaround: std utility functions aren't constexpr yet
template <class T> inline constexpr T&& constexpr_forWARD(typename 
std::remove_reference<T>::type& t) noexcept
{
    return static_cast<T&&>(t);
}

所以我不会通过以下方式理解这一点:

> typename在这做什么?只是声明以下部分是一种类型?
>为什么我们需要std :: remove_reference?我们没有在类型&中添加引用.部分?
>“std实用功能还不是constexpr”是什么意思?这个函数如何使它们成为constexpr?它的主体只是一个static_cast.
>此函数在许多构造函数中使用,它看起来像这样:template< class ... Args>
constexpr storage_t(Args&& … args):value_(constexpr_forWARD< Args>(args)…){},那么它对args做了什么?

非常感谢.

解决方法

template <class T> inline constexpr T&& constexpr_forWARD(typename 
std::remove_reference<T>::type& t) noexcept
{
    return static_cast<T&&>(t);
}

std :: remove_reference< T> :: type是一个依赖类型,它取决于模板参数T,因此我们需要typename来告诉编译器我们正在尝试使用dependent-name,

如果您在here中检查此实用程序功能的示例用法

....
template <class... Args> explicit constexpr constexpr_optional_base(in_place_t,Args&&... args)
      : init_(true),storage_(constexpr_forWARD<Args>(args)...) {}
...

你可以看到,一个可变参类型被用作constexpr_foWARD< Args>(args)的显式模板参数….这将保留该类型的value category.当任何参数是引用时,就好像我们使用COnstexpr_forWARD< Arg01&>(arg01)调用该实用程序函数一样.并且该模板的实例化将是

inline constexpr Arg01&&& constexpr_forWARD(Arg01& t) noexcept
{
    return static_cast<Arg01&&&>(t);
}

到了reference collapsing rule,我们有

inline constexpr Arg01& constexpr_forWARD(Arg01& t) noexcept
{
    return static_cast<Arg01&>(t);
}

实际上,删除引用应该是多余的(阅读参折叠);

它只是forwarding constexpr函数和构造函数中的非constexpr函数.

基本上如上所述..

大佬总结

以上是大佬教程为你收集整理的const_forward在C中的可选实现中做了什么?全部内容,希望文章能够帮你解决const_forward在C中的可选实现中做了什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。