什么是 std::identity 以及它是如何使用的? [英] what is std::identity and how it is used?

查看:38
本文介绍了什么是 std::identity 以及它是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道 std::identity 的目的是什么?我在网上找不到任何有用的东西.我知道它是如何实现的:

I just want to know what is the purpose of std::identity? I could not find anything useful on web. I know how it is implemented :

template <typename T>
struct identity
{
    T operator()(T x) const { return x; }
};

为什么我们真的需要这个?

why do we actually need this?

推荐答案

Standard (up to C++20) 没有 std::identity,所有提到它的提案都被删除了.最初建议时,它应该用于与接受标准中的 std::forward 服务相同的目的,但它与非标准扩展相冲突,并且经过多次迭代最终被删除.

Standard (up to C++20) doesn't have std::identity, all proposals mentioning it have been removed. When initially suggested, it was supposed to serve the same purpose as std::forward serves in accepted standard, but it conflicted with non-standard extensions and after several iterations was finally removed.

C++20 支持 std::identity:https://en.cppreference.com/w/cpp/utility/functional/identity

这篇关于什么是 std::identity 以及它是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆