forward计算机语言,const_forward在C中的可选实现中做了什么?

嗨我在

here看到std :: optional实现之一,我发现这段代码让我感到困惑:

// workaround: std utility functions aren't constexpr yet

template inline constexpr T&& constexpr_forward(typename

std::remove_reference::type& t) noexcept

{

return static_cast(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 inline constexpr T&& constexpr_forward(typename

std::remove_reference::type& t) noexcept

{

return static_cast(t);

}

What does typename do here? Just to declare the following part is a type?

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

Why do we need std::remove_reference here? Didn’t we add the reference back in the type& part?

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

....

template explicit constexpr constexpr_optional_base(in_place_t, Args&&... args)

: init_(true), storage_(constexpr_forward(args)...) {}

...

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

inline constexpr Arg01&&& constexpr_forward(Arg01& t) noexcept

{

return static_cast(t);

}

到了reference collapsing rule,我们有

inline constexpr Arg01& constexpr_forward(Arg01& t) noexcept

{

return static_cast(t);

}

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

What does “std utility functions are not constexpr yet” mean? How does this function make them constexpr? The body of it is just a static_cast.

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

This function is used in a number of constructors and it looks like this: template

constexpr storage_t( Args&&... args ) : value_(constexpr_forward(args)...) {}, so what does it do here to args?

基本上如上所述..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值