perfect-forwarding

Why does std::forward discard constexpr-ness?

北城以北 提交于 2019-11-30 01:49:10
问题 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

Why does a perfect forwarding function have to be templated?

北战南征 提交于 2019-11-29 16:48:42
问题 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

A failure to instantiate function templates due to universal (forward) reference to a templated type

人盡茶涼 提交于 2019-11-29 09:31:08
Universal references (i.e. "forward references", the c++ standard name) and perfect forwarding in c++11 , c++14 , and beyond have many important advantages; see here , and here . In Scott Meyers' article referenced above ( link ), it is stated as a rule of thumb that: If a variable or parameter is declared to have type T&& for some deduced type T, that variable or parameter is a universal reference. Example 1 Indeed, using clang++ we see that the following code snippet will successfully compile with -std=c++14 : #include <utility> template <typename T> decltype(auto) f(T && t) { return std:

When should I std::forward a function call?

眉间皱痕 提交于 2019-11-29 09:05:13
问题 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

is any difference between std::forward<T> and std::forward<decltype(t)>?

纵饮孤独 提交于 2019-11-28 23:13:53
are these functions equivalent? template <class T> void foo(T && t) { bar(std::forward<T>(t)); } template <class T> void foo2(T && t) { bar(std::forward<decltype(t)>(t)); } template <class T> void foo3(T && t) { bar(std::forward(t)); } if they are, can I always use this macro for perfect forwarding? #define MY_FORWARD(var) std::forward<decltype(var)>(var) or just use bar(std::forward(t)); I believe foo2 and foo3 are same, but I found people are always use forward like foo , is any reason to explicitly write the type? I understand that T and T&& are two different types, but I think std::forward

What is the best way of renaming (alias/forward) a function in C++?

牧云@^-^@ 提交于 2019-11-28 19:24:33
(I'll restrict this question to C++11, since I believe there is no general way to do this in C++98). Supposed I have a complicated (in terms of signature) set of template functions and/or overloaded functions, and I want to use these functions in the exact same way but using a different name (that is, an alias). For example: template<class A, class B, class C> D fun(A a, B& b, C&& c){ ... } template<class E, class F> G fun(H<E> he, F& f){ ... } ... many other versions of fun Now suppose that I want to rename (or alias , or forward to be more precise) these functions, all at once (i.e. be able

Why use a perfectly forwarded value (a functor)?

一个人想着一个人 提交于 2019-11-28 17:00:59
C++11 (and C++14) introduces additional language constructs and improvements that target generic programming. These include features such as; R-value references Reference collapsing Perfect forwarding Move semantics, variadic templates and more I was browsing an earlier draft of the C++14 specification (now with updated text) and the code in an example in §20.5.1, Compile-time integer sequences , that I found interesting and peculiar. template<class F, class Tuple, std::size_t... I> decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) { return std::forward<F>(f)(std::get<I>(std:

How would one call std::forward on all arguments in a variadic function?

Deadly 提交于 2019-11-28 15:56:14
I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me thinking...when C++0X comes out and I had a standard compiler I would do this with real variadic templates. How though, would I call std::forward on the arguments? template <typename ...Params> void f(Params... params) // how do I say these are rvalue reference? { y(std::forward(...params)); //? - I doubt this would work. } Only way I can think of

Why does std::forward have two overloads?

江枫思渺然 提交于 2019-11-28 10:53:55
Given the following reference collapsing rules T& & --> T& T&& & --> T& T& && --> T& T&& && --> T&& The third and fourth rule imply that T(ref qualifer) && is the identity transformation, i.e. T& stays at T& and T&& stays at T&& . Why do we have two overloads for std::forward ? Couldn't the following definition serve all purposes? template <typename T, typename = std::enable_if_t<!std::is_const<T>::value>> T&& forward(const typename std::remove_reference<T>::type& val) { return static_cast<T&&>(const_cast<T&&>(val)); } Here the only purpose the const std::remove_reference<T>& serves is to not

How to combine std::bind(), variadic templates, and perfect forwarding?

百般思念 提交于 2019-11-28 08:37:21
I want to invoke a method from another, through a third-party function; but both use variadic templates. For example: void third_party(int n, std::function<void(int)> f) { f(n); } struct foo { template <typename... Args> void invoke(int n, Args&&... args) { auto bound = std::bind(&foo::invoke_impl<Args...>, this, std::placeholders::_1, std::forward<Args>(args)...); third_party(n, bound); } template <typename... Args> void invoke_impl(int, Args&&...) { } }; foo f; f.invoke(1, 2); Problem is, I get a compilation error: /usr/include/c++/4.7/functional:1206:35: error: cannot bind ‘int’ lvalue to