perfect-forwarding

implementing a variadic zip function with const-correctness

限于喜欢 提交于 2019-11-30 22:35:43
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 namespace ns { template<typename... Types> std::tuple<Types&&...> forward_as_tuple(Types&&... t) { return std:

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

笑着哭i 提交于 2019-11-30 22:34:47
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 testFunc(T && t) { [test = std::forward<T>(t)](){}(); } int main() { testClass x; std::cout << "PLEASE

Why does std::forward discard constexpr-ness?

守給你的承諾、 提交于 2019-11-30 17:45:28
Being not declared constexpr , std::forward will discard constexpr-ness for any function it forwards arguments to. Why is std::forward not declared constexpr itself so it can preserve constexpr-ness? Example: (tested with g++ snapshot-2011-02-19) #include <utility> template <typename T> constexpr int f(T x) { return -13;} template <typename T> constexpr int g(T&& x) { return f(std::forward<T>(x));} int main() { constexpr int j = f(3.5f); // next line does not compile: // error: ‘constexpr int g(T&&) [with T = float]’ is not a constexpr function constexpr int j2 = g(3.5f); } Note: technically,

std::bind and perfect forwarding

大兔子大兔子 提交于 2019-11-30 17:11:11
问题 The following code does not compile: #include <functional> template<class ...Args> void invoke(Args&&... args) { } template<class ...Args> void bind_and_forward(Args&&... args) { auto binder = std::bind(&invoke<Args...>, std::forward<Args>(args)...); binder(); } int main() { int a = 1; bind_and_forward(a, 2); } If I understand correctly, the reason is as follows: std::bind copies its arguments, and when the binder 's operator() is called, it passes all the bound arguments as lvalues - even

Why does a perfect forwarding function have to be templated?

我是研究僧i 提交于 2019-11-30 11:12:16
Why is the following code valid: template<typename T1> void foo(T1 &&arg) { bar(std::forward<T1>(arg)); } std::string str = "Hello World"; foo(str); // Valid even though str is an lvalue foo(std::string("Hello World")); // Valid because literal is rvalue But not: void foo(std::string &&arg) { bar(std::forward<std::string>(arg)); } std::string str = "Hello World"; foo(str); // Invalid, str is not convertible to an rvalue foo(std::string("Hello World")); // Valid Why doesn't the lvalue in example 2 get resolved in the same manner that it does in example 1? Also, why does the standard feel it

Move semantics and perfect forwarding difference

筅森魡賤 提交于 2019-11-30 09:09:39
I already got what move semantics is from this question: What are move semantics? But I still do not get what perfect forwarding is in relation to move semantics. Can someone explain in simple english and with a simple example what perfect forwarding means? Plain English-only attempt The problem is probably too complex to be accurately described by plain English sentences, but one could think of perfect forwarding as a way to move temporary values passed to a function to another one as if the first function didn't exist at all , so without any unnecessary copies or assignments. C++11 allows

Proper style for declaration in range-based for

不羁岁月 提交于 2019-11-30 08:47:37
This question mentioned the obvious, idiomatic usage of C++11 range-based for. for (auto& elem: container) { // do something with elem } I've been having doubts about the kind of reference you're supposed to use, though. Input iterators may return rvalues. Although the implicit type introduced by auto could be deduced to const which would bind to an rvalue, that doesn't seem to occur. Is the best general practice to use perfect forwarding? for (auto && elem: container) { // do something with elem } I see no downside here, but it looks a little too cute. Maybe I still just haven't written

When should I std::forward a function call?

不问归期 提交于 2019-11-30 07:12:41
A code snippet I saw in Effective Modern C++ has a clever implementation of the instrumentation rationale to create a function timer : auto timeFuncInvocation = [](auto&& func, auto&&... params) { start timer; std::forward<decltype(func)>(func)( std::forward<decltype(params)>(params)...); stop timer and record elapsed time; }; My question is about std::forward<decltype(func)>(func)(... To my understanding, we are actually casting the function to its original type, but why is this needed? It looks like a simple call would do the trick. Are there any other cases where we use perfect forwarding

How does std::forward receive the correct argument?

不问归期 提交于 2019-11-30 06:38:10
问题 Consider: void g(int&); void g(int&&); template<class T> void f(T&& x) { g(std::forward<T>(x)); } int main() { f(10); } Since the id-expression x is an lvalue, and std::forward has overloads for lvalues and rvalues, why doesn't the call bind to the overload of std::forward that takes an lvalue? template<class T> constexpr T&& forward(std::remove_reference_t<T>& t) noexcept; 回答1: It does bind to the overload of std::forward taking an lvalue: template <class T> constexpr T&& forward(remove

What's the correct `enable_if` constraint on perfect forwarding setter?

Deadly 提交于 2019-11-30 04:51:56
Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance in all the cases tested, but too difficult for most developers to write) was perfect forwarding, with the example given (PDF, pg. 28) : class employee { std::string name_; public: template <class String, class = std::enable_if_t<!std::is_same<std::decay_t<String>, std::string>::value>> void set_name(String &&name) noexcept( std::is_nothrow_assignable