perfect-forwarding

Syntax differences in variadic template parameter pack forwarding

巧了我就是萌 提交于 2019-12-23 15:36:39
问题 While working with variadic templates I have come across two different ways of writing a call to std::forward , but I am left wondering what the actual difference between the two syntaxes? template<typename... T> void one(T&&... args) { foo(std::forward<T&&...>(args...)); } template<typename... T> void two(T&&... args) { foo(std::forward<T&&>(args)...); } According to my compilers these are both valid syntax, and in most cases the compiler does not complain. But I have found some cases where

boost::range::join many ranges in one custom call

烈酒焚心 提交于 2019-12-23 02:38:59
问题 The Solution section in gnzlbg's SO question boost::range::join for multiple ranges implies it can join many ranges in one client code call to a custom function variadic template that calls boost::join and boost::make_iterator_range . According to that question, answer, and comments, the prior can join 2 ranges and the latter is needed to ensure the non- const overload of the prior is used. Any containers after the 2nd one are supposedly perfect-forwarded via std::forward . But my client code

How to implement perfect forwarding on a non-generic type?

不打扰是莪最后的温柔 提交于 2019-12-23 01:24:13
问题 say I have the following code: class Element; typedef shared_ptr<Element> ElementPtr; class Element { public: void add_child(const ElementPtr& elem); private: vector<ElementPtr> children; } inline void Element::add_child(const ElementPtr& elem) { children.push_back(elem); }; And I want to update add_child to use perfect forwarding. I tried changing the function definition (and declaration) so use the following logic: void Element::add_child(ElementPtr&& elem) { children.push_back(forward

Capturing generic callable objects in nested lambdas - always forward?

有些话、适合烂在心里 提交于 2019-12-22 18:01:25
问题 I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both

How to store universal references

情到浓时终转凉″ 提交于 2019-12-22 04:47:07
问题 I need to store universal references inside a class (I am sure the referenced values will outlive the class). Is there a canonical way of doing so? Here is a minimal example of what I have come up with. It seems to work, but I'm not sure if I got it right. template <typename F, typename X> struct binder { template <typename G, typename Y> binder(G&& g, Y&& y) : f(std::forward<G>(g)), x(std::forward<Y>(y)) {} void operator()() { f(std::forward<X>(x)); } F&& f; X&& x; }; template <typename F,

Why doesn't `const int ci = 2; std::forward<int>(ci);` work and how to fix / workaround it?

别来无恙 提交于 2019-12-22 04:03:52
问题 Simple question, why doesn't the following work (implying a copy of ci )? #include <utility> int main(){ const int ci = 2; std::forward<int>(ci); } prog.cpp: In function 'int main()': prog.cpp:6:23: error: no matching function for call to 'forward(const int&)' The problem manifested itself while writing some template stuff, where I have a simple holder type as follows. To avoid unnecessary copies, I use perfect forwarding where possible, but that turns out to be the root of the problem it

detecting protected constructors of (possibly abstract) base class

99封情书 提交于 2019-12-21 03:29:17
问题 I am experimenting with the new features of C++11. In my setup I would really love to use inheriting constructors, but unfortunately no compiler implements those yet. Therefore I am trying to simulate the same behaviour. I can write something like this: template <class T> class Wrapper : public T { public: template <typename... As> Wrapper(As && ... as) : T { std::forward<As>(as)... } { } // ... nice additions to T ... }; This works... most of the time. Sometimes the code using the Wrapper

Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference

橙三吉。 提交于 2019-12-19 08:53:22
问题 I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned into a lvalue reference by the universal reference function template. Why is it so? #include <iostream> #include <type_traits> using namespace std; template <typename T> T f1(T&&t) { //<-----this is a universal reference cout << "is_lvalue reference:" << is_lvalue_reference<T>::value << endl; cout <

implementing a variadic zip function with const-correctness

北城以北 提交于 2019-12-19 03:44:06
问题 I'm trying to implement a zip function. zip 's parameters are each wrapped<Ti> , where Ti varies from parameter to parameter. zip takes these wrapped<Ti> s and produces a wrapped<tuple<T1&,T2&,...TN&>> , or in other words a wrapped tuple of references to its parameters. The references should preserve const -ness. Here's my first stab at zip with one parameter, which doesn't work in general: #include <utility> #include <tuple> // implement forward_as_tuple as it is missing on my system

Perfectly capturing a perfect forwarder (universal reference) in a lambda

不想你离开。 提交于 2019-12-19 03:41:46
问题 So I have a perfect forwarder, and I want to appropriately capture it in a lambda, such that R-values are copied in, and L-values are captured by reference. However simply using std::forward doesn't do the job, as evidenced by this code: #include<iostream> class testClass { public: testClass() = default; testClass( const testClass & other ) { std::cout << "COPY C" << std::endl; } testClass & operator=(const testClass & other ) { std::cout << "COPY A" << std::endl; } }; template< class T> void