- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在阅读cppreference page on Constraints并注意到以下示例:
// example constraint from the standard library (ranges TS)
template <class T, class U = T>
concept bool Swappable = requires(T t, U u) {
swap(std::forward<T>(t), std::forward<U>(u));
swap(std::forward<U>(u), std::forward<T>(t));
};
std::forward
。是否尝试在模板参数中支持引用类型?我们是否不想用左值调用
swap
,并且当
forward
和
T
是标量(非引用)类型时,
U
表达式不是右值吗?
Swappable
实现,我希望该程序失败:
#include <utility>
// example constraint from the standard library (ranges TS)
template <class T, class U = T>
concept bool Swappable = requires(T t, U u) {
swap(std::forward<T>(t), std::forward<U>(u));
swap(std::forward<U>(u), std::forward<T>(t));
};
class MyType {};
void swap(MyType&, MyType&) {}
void f(Swappable& x) {}
int main()
{
MyType x;
f(x);
}
T
和
U
都应为
MyType
,而
std::forward<T>(t)
应返回
MyType&&
,但无法将其传递给我的
swap
函数。
Swappable
的此实现是否错误?我错过了什么吗?
最佳答案
Don't we want to call swap with lvalues […]
- An object
t
is swappable with an objectu
if and only if:
- […] the expressions
swap(t, u)
andswap(u, t)
are valid […][…]
An rvalue or lvalue
t
is swappable if and only if t is swappable with any rvalue or lvalue, respectively, of typeT
.
Swappable
requirements [swappable.requirements],以减少许多不相关的细节。)
template<typename Lhs, typename Rhs = Lhs>
concept bool FirstKindOfSwappable = requires(Lhs lhs, Rhs rhs) {
swap(lhs, rhs);
swap(rhs, lhs);
};
int&& a_rref = 0;
int&& b_rref = 0;
// valid...
using std::swap;
swap(a_rref, b_rref);
// ...which is reflected here
static_assert( FirstKindOfSwappable<int&&> );
int&&
是我们的变量的声明类型,以及传递给概念的实际参数,继而又以我们的
lhs
和
rhs
的声明类型需要参数而结束。当我们深入研究时,请记住这一点。
decltype
以及另一个方向上的
std::declval
使用的那个。这导致我们:
template<typenaome Lhs, typename Rhs = Lhs>
concept bool SecondKindOfSwappable = requires(Lhs lhs, Rhs rhs) {
swap(std::forward<Lhs>(lhs), std::forward<Rhs>(rhs));
swap(std::forward<Rhs>(rhs), std::forward<Lhs>(lhs));
// another way to express the first requirement
swap(std::declval<Lhs>(), std::declval<Rhs>());
};
// not valid
//swap(0, 0);
// ^- rvalue expression of type int
// decltype( (0) ) => int&&
static_assert( !SecondKindOfSwappable<int&&> );
// same effect because the expression-decltype/std::declval encoding
// cannot properly tell apart prvalues and xvalues
static_assert( !SecondKindOfSwappable<int> );
int a = 0, b = 0;
swap(a, b);
// ^- lvalue expression of type int
// decltype( (a) ) => int&
static_assert( SecondKindOfSwappable<int&> );
int
的左值表达式,该表达式被编码为该概念的
int&
参数,并通过
std::declval<int&>()
恢复为我们约束中的表达式。或者通过
std::forward<int&>(lhs)
以更round回的方式。
Swappable
概念的摘要。如果我猜到了,我会说Ranges TS决定提供
Swappable
参数来表示表达式,原因如下:
SecondKindOfSwappable
来编写FirstKindOfSwappable
,如下所示:template<typename Lhs, typename Rhs = Lhs>
concept bool FirstKindOfSwappable = SecondKindOfSwappable<Lhs&, Rhs&>;
swap(std::forward<Lhs>(lhs), std::forward<Rhs>(rhs))
的
template<typename Val, typename It>
void client_code(Val val, It it)
requires Swappable<Val&, decltype(*it)>
// ^^^^^^^^^^^^^--.
// |
// hiding an expression into a type! ------`
{
ranges::swap(val, *it);
}
DerivedFrom<Derived, Base>()
,它的值通常不会提供有效的表达式(或使用变量的方式)。
Constructible<Arg, Inits...>()
第一个参数
Arg
可以用两种方式解释:
Arg
代表一种类型,即将可构造性作为Arg
是正在构造的变量的声明类型,即约束表示Arg imaginary_var { std::declval<Inits>()... };
是有效的关于c++ - 为什么在概念中使用std::forward?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356761/
以下操作是否完全相同: request.setAttribute(name,"visited"); requestDispatcher.forward(request,response) 和
有两个 header 用于处理请求转发。首先是 X-Forwarded-For第二个是 Forwarded . 我应该使用哪一个? X-Forwarded-For 是事实上的,而 Forwarded
看the documentation for std::forward , template constexpr T&& forward( typename std::remove_reference
给定一个手电筒的nn.Module带有预前钩,例如 import torch import torch.nn as nn class NeoEmbeddings(nn.Embedding):
为了在我的 ASP.NET 应用程序中获取客户端 IP 地址,我使用了 X-Forwarded-For,并从列表中获取第一个 IP 地址(根据我找到的信息,有一个客户端,代理1、代理2..)。但我最近
以下 header 有什么区别? X 转发主机 X 转发服务器 在 Apache 作为代理、Tomcat 作为源服务器的设置中,我在到达 Tomcat 的 HTTP 请求中看到它们。值是(这是我自己的
假设我想要一个通用的高阶函数。最常见的方法是: 一个。通用 lambda auto F1 = [](auto f) { /*...*/ } B.函数模板 template auto F2(F&& f)
为了在我的 ASP.NET 应用程序中获取客户端 IP 地址,我使用了 X-Forwarded-For,并从列表中获取第一个 IP 地址(根据我找到的信息,有一个客户端,代理 1、代理 2 ..)。但
我有一个场景,我想使用 Context.Forward 将上下文转发到另一个对话框,它工作得很好。现在,如果我更新代码以获取对话框堆栈并使用 DialogStack.Forward,则会导致 Stac
我遇到了 std::forward 的两种变体与可变参数模板参数一起使用。 template void foo(Args&&... arga) { bar(std::forward(args
考虑 std::apply 的可能实现: namespace detail { template constexpr decltype(auto) apply_impl(F &&f, Tuple &
我在 Foo Controller 中有一个需要参数的 Action 方法: public function fooAction($one, $two) { $a = one; $b
这些功能是否等效? template void foo(T && t) { bar(std::forward(t)); } template void foo2(T && t) {
在建立从 Fedora 23 到 Centos 7 的 ssh 连接时,我遇到了一个不幸的问题。命令 ssh -X user@centos7和 ssh -Y user@centos7都打印 警告:没有
作为从 Struts 1.x 迁移到 Spring Boot 的一部分,我一直在努力正确转发应有的信息。 最初的 Struts 版本依赖于 web.xml,尽管我想将该项目转换为更新的、完全基于 Ja
我使用了net = cv2.dnn.readNetFromCaffe(protoFile, WeightsFile),然后循环播放实时视频帧以使用net.forward()") bre
我注册了域名 www.a.com 然后我将屏蔽(使用 GoDaddy)流量从 www.a.com 转发到 www.really.long.url.com,因此用户在访问我的网站时只会在地址栏中看到 w
我有一个域,例如 domain.org以及地址为 http://server/node/myname 的服务器上的托管空间. 我如何映射 domain.org至 http://server/node/
我有jsp,具体取决于我正在转发到另一个页面的某些条件。 我有/myjsp/first.jsp 其代码如下, 在这段代码中,if flow come to else 条件控
前向声明和前向引用有什么区别? 在我看来,前向声明是当你声明一个尚未实现的函数时,但这不正确吗?您是否必须查看特定情况以声明案例“前向引用”或“前向声明”? 最佳答案 一个 前向声明是在实现和使用方法
我是一名优秀的程序员,十分优秀!