perfect-forwarding 相关问题

完美转发描述了C ++ 11函数模板的一个属性,它允许正确地将参数推导为左值或右值,并将它们以相同的形式转发给其他函数。

特定类型完美转发

在编写线程安全的 std::stack 包装器时,我为推送做了以下两个重载: 无效推送(const value_type&value) { 自动防护= std::scoped_lock{_mutex}; _stack.push(值);...

回答 2 投票 0

std::forward 的第二次重载(cppreference.com 上的示例)

我知道 std::forward 的第二次重载: 模板< class T > constexpr T&& 向前( std::remove_reference_t&& t ) noexcept; 用于右值(如

回答 1 投票 0

是否有使用 std::forward,其中参数不是变量名?

在大多数情况下,std::forward 的参数类型是转发引用: 模板 void CallF(T&& 值) { F(std::forward(值)); } 在极少数情况下,类型...

回答 1 投票 0

C++ 如何允许复制列表初始化以实现完美的转发功能?

假设我有一个完善的转发功能: 模板 void f(T&& t) { /* 做某事 */ } 我有特定类型 T,我想允许对其进行复制列表初始化,s...

回答 1 投票 0

为什么一个完美的转发功能一定要模板化?

为什么下面的代码有效: 模板 void foo(T1 &&arg) { bar(std::forward(arg)); } std::string str = "你好世界"; foo(str); // 即使如此也有效...

回答 1 投票 0

这是完美的转发功能吗?

这是一个完美的转发功能吗?或者临时T的创建是否会使其变得不完美? T 是一个平凡的类。 模板 void foo(参数&&...参数) { ...

回答 1 投票 0

是否有相当于 std::forward 的移动或复制功能,可以在 std::ranges 中完美转发?

如何将以下两个功能合二为一?有没有类似于 std::forward 的东西,但是对于范围? #包括 #包括 #包括 寺庙...

回答 1 投票 0

为什么我不能以`void(*func)(T&&)`和`for (T&& v : vec)`这样的方式使用通用引用?

我试图在函数指针和基于范围的for循环中使用通用引用。 模板 void PrintFunc(const T& 值) { 计算<< value << "\n&q...

回答 1 投票 0

C++ - 移动构造和转发之间的交互

如果我正在构建一个不平凡的容器,例如自定义地图, 并且想要在 emplace 和 insert 函数之间共享代码,一种方法是简单地使用 insert 函数...

回答 1 投票 0

std::is_convertible 中的 std::decay 是多余的吗?

我写了这样的代码: 模板 A类{ 模板 ,std::decay_t>>> ...

回答 1 投票 0

我什么时候想使用 auto&& 而不是 decltype(auto) 或 ->decltype(return-expr) 作为函数定义的返回类型?

采用三个返回纯右值、左值、x值的函数: int f(); int&f(int); int&& f(int, int); 并通过返回 decltype(auto) 的函数调用它们 decltype(auto) 返回DecltypeOf(

回答 1 投票 0

在循环中对同一参数使用 std::forward

我知道在某些情况下,在循环中对同一参数使用 std::forward 是错误的,因为它可能会导致从移动的对象移动,如下所示: 模板 自动应用十次(T&...

回答 1 投票 0

std::forward() 的右值引用重载的目的是什么? [重复]

我正在尝试完美转发,我发现 std::forward() 需要两个重载: 过载编号1: 模板 内联 T&& 转发(类型名称 std::remove_refere...

回答 1 投票 0

std::forward 与 std::move 的用法[重复]

我总是读到 std::forward 仅适用于模板参数。然而,我问自己为什么。请参见以下示例: void ImageView::setImage(const Image& image){ _图像 =

回答 3 投票 0

为什么 std::forward 在传输左值时返回右值? [重复]

据说std::forward()完美地将左值转发到左值,将右值转发到右值。但下面的代码似乎显示出不同的结论? 当我运行下面的代码时 #包括 据说 std::forward() 完美将左值转发到左值,将右值转发到右值。但下面的代码似乎显示出不同的结论? 当我运行下面的代码时 #include <iostream> class A { public: int x; A() {x = 0;} explicit A(int x) : x(x) {} A& operator= (A&& other) { std::cout << " && =" << std::endl; x = other.x; } A& operator= (const A& other) { std::cout << " & =" << std::endl; x = other.x; } }; int main() { A p1(1), p2, p3; p2 = p1; p3 = std::forward<A>(p1); return 0; } 结果是: & = && = 看起来 std::forward<A>(p1) 将 p1 作为右值转发。 std::forward()的源代码是: /** * @brief Forward an lvalue. * @return The parameter cast to the specified type. * * This function is used to implement "perfect forwarding". */ template<typename _Tp> constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type& __t) noexcept { return static_cast<_Tp&&>(__t); } /** * @brief Forward an rvalue. * @return The parameter cast to the specified type. * * This function is used to implement "perfect forwarding". */ template<typename _Tp> constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type&& __t) noexcept { static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument" " substituting _Tp is an lvalue reference type"); return static_cast<_Tp&&>(__t); } gdb 显示,当运行 p3 = std::forward<A>(p1); 时,它会进入第一个函数 forward(typename std::remove_reference<_Tp>::type& __t) noexcept 并返回类型为 A && 的值 (gdb) p __t $2 = (std::remove_reference<A>::type &) @0x7fffffffd1cc: { x = 1 } (gdb) p static_cast<_Tp&&>(__t) $3 = (A &&) @0x7fffffffd1cc: { x = 1 } 我完全困惑了。您能解释一下调用 std::forward() 时发生了什么吗?如果 std::forward<A>(p1) 将 p1 作为右值转发,那么 std::forward 和 std::move 有什么区别? 如果 std::forward<A>(p1) 将 p1 作为右值转发,那么 std::forward 和 std::move 有什么区别? 区别在于 std::forward<A&>(p1) 返回左值,而 std::move 则不返回。 你误用了std::forward。传递常量模板类型参数是没有意义的。当函数具有从函数参数推导出类型的转发引用时,它们非常有用:示例: template<class T> void foo(T&& ref) { T t = std::forward<T>(ref); // copy or move ... foo(p1); // T is deduced as A&

回答 1 投票 0

有没有办法描述参数不会通过转发引用发生变异?

const 限定的众多好处之一是使 API 更易于理解,例如: 模板 int function1(T const& in); // 显然,输入不会通过

回答 2 投票 0

完美转发混乱

大家好, 模板 int mycall(const X& a) { std::cout << ">>> " << "int mycall(const X& a)" << std::endl; ret...

回答 2 投票 0

C++概念与完美转发

我对容器类型有一个相对简单的概念定义,它接受特定的值类型: 模板 概念 FooContainer = 需要(T cont){ std::开始(续); ...

回答 1 投票 0

转发容器的价值类别?

在 C++20 中,我想编写通用代码来获取容器或视图并完美地转发每个元素。也就是说,如果我有这样一个令人讨厌的类型: 结构 S { 结构标签{}; // 所以我们有一个...

回答 0 投票 0

如何完美转发一个 const ref 或 movable rvalue 的通用引用?

我已经用 C++20 编写了一个无锁的线程安全环形队列,到目前为止它可以正常工作。 唯一不完美的是它必须有两个 enque() 方法,一个接受对左值的 const 引用作为

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.