perfect-forwarding

In std::forward how does it accept rvalue?

谁都会走 提交于 2019-12-11 02:40:30
问题 Looking at Scott Meyer's Effective Modern C++ pages 200-201, the suggested simplified implementation of std::forward could be (did see the proper implementation elsewhere): template <typename T> T&& forward(std::remove_reference_t<T>& param) { return static_cast<T&&>(param); } And when accepting an rvalue Widget, it becomes: Widget&& forward(Widget& param) { return static_cast<Widget&&>(param); } Now, if you take that substituted code, and do: struct Widget { }; Widget&& forward(Widget& param

Why can't I use traits with forwarding references in C++?

我的梦境 提交于 2019-12-10 14:14:28
问题 I have the following test code. See godbolt https://godbolt.org/z/fLRM8d for an executable example template <typename T> struct Traits { static const bool value = false; }; struct Zip{}; template <> struct Traits<Zip> { static const bool value = true; }; template <typename E> void Execute(E && e){ static_assert(Traits<E>::value); } int main(){ auto z = Zip(); // Fails the static assertion with an lvalue Execute(z); // Passes the static assertion with an rvalue Execute(Zip()); } What is going

Conflict between perfect forwarding constructor and copy constructor in class hierarchy

拈花ヽ惹草 提交于 2019-12-10 12:58:16
问题 I recently encountered a problem while trying to implement a class hierarchy with perfect forwarding constructors. Consider the following example: struct TestBase { template<typename T> explicit TestBase(T&& t) : s(std::forward<T>(t)) {} // Compiler refers to this line in the error message TestBase(const TestBase& other) : s(other.s) {} std::string s; }; struct Test : public TestBase { template<typename T> explicit Test(T&& t) : TestBase(std::forward<T>(t)) {} Test(const Test& other) :

C++11 perfect forwarding and reference collapsing

左心房为你撑大大i 提交于 2019-12-10 11:49:27
问题 Consider this code: template<typename T> void foo(T&& param){ //In this case && is called universal reference std:string tmp = std::forward<string>(param); } My question is if universal reference type can be deduced why do I still need to call forward ? Why without forwarding tmp's correct c'tor won't be called even if T's type was deduced. My second question is about reference collapsing rules: A& && becomes A& A&& && becomes A&& so according this rules and taking in account universal

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

拥有回忆 提交于 2019-12-10 04:31:05
问题 I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&& ). Thus,to allow only rvalues, one need to write template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes> void foo(T&& x) {} and not, template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes> void foo(T&& x) {} My question is , why for forwarding references, rvalues are deduced to be of type T and

Proper use of universal references

守給你的承諾、 提交于 2019-12-10 03:43:57
问题 Before c++11, I used to write code like this: // Small functions void doThingsWithA(const A& a) { // do stuff } void doThingsWithB(const B& b) { // do stuff } void doThingsWithC(const C& c) { // do stuff } // Big function void doThingsWithABC(const A& a, const B& b, const C& c) { // do stuff doThingsWithA(a); doThingsWithB(b); doThingsWithC(c); // do stuff } But now, with move semantics, it may become interesting (at least in some cases) to allow my functions to take rvalue references as

std::forward without perfect forwarding?

元气小坏坏 提交于 2019-12-10 01:24:04
问题 Advice on std::forward is generally limited to the canonical use case of perfectly forwarding function template arguments; some commentators go so far as to say this is the only valid use of std::forward . But consider code like this: // Temporarily holds a value of type T, which may be a reference or ordinary // copyable/movable value. template <typename T> class ValueHolder { public: ValueHolder(T value) : value_(std::forward<T>(value)) { } T Release() { T result = std::forward<T>(value_);

Why can't I return a unique_ptr from a pair?

淺唱寂寞╮ 提交于 2019-12-09 17:23:45
问题 Why can't I return a unique_ptr from a pair? #include <iostream> #include <memory> #include <utility> using namespace std; unique_ptr<int> get_value() { pair<unique_ptr<int>, int> p(unique_ptr<int>(new int(3)), 4); return p.first; } int main(void) { cout << *get_value() << endl; return 0; } When I try to compile this with g++ 4.6, I get: ../main.cpp: In function ‘std::unique_ptr<int> get_value()’: ../main.cpp:9:11: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const

C++11 std::function and perfect forwarding

梦想与她 提交于 2019-12-09 08:28:42
问题 Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not R operator()(ArgTypes&&...) const; ? One would think that to correctly forward parameters, we need the && and then use std::forward<ArgTypes>... in the function body when forwarding the call? I partially reimplemented std::function to test this and I found out that if I use the &&, I get "cannot bind 'xxx' lvalue to 'xxx&&'" from g++ when I try later to pass parameters by value to

How remove_reference disable template argument deductions?

给你一囗甜甜゛ 提交于 2019-12-08 21:22:47
问题 According to this link, template argument deduction disallowed for std::forward and std::remove_reference is helping us to achieve that. But how does using remove_reference prevent template deduction from happening here? template <class S> S&& forward(typename std::remove_reference<S>::type& t) noexcept { return static_cast<S&&>(t); } 回答1: S in the expression typename std::remove_reference<S>::type is a non-deduced context (specifically because S appears in the nested-name-specifier of a type