perfect-forwarding

Forwarding of return values. Is std::forward is needed?

拟墨画扇 提交于 2019-12-02 23:31:14
I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template<class T> T&& wrapper(T&& t) { f(t); // t passed as lvalue return std::forward<T>(t); } f returns void and takes T&& (or overloaded on valueness). Wrapper always returns wrappers's param and on returned value should preserve valuness of argument. Do I actually need to use std::forward in return ? Does RVO makes it superfluous? Does the fact that it is a reference (R or L) makes it superfluous? Is it needed if return is not last

Perfect-forward non-T arguments while converting T-s

自作多情 提交于 2019-12-02 13:56:56
问题 (This question follows from this answer) I am trying to adapt a trampoline function that is currently just passing through a variable number of arguments. I would like to have it convert any argument PyObject* pyob to Object{pyob} , but forward all other arguments through. So (void* self, int, PyObject*, float) -> (int, Object, float) In that example, the first self argument is stripped away. This always happens. Out of the remaining arguments, one of them is of type PyObject*, and hence

Perfect-forward non-T arguments while converting T-s

冷暖自知 提交于 2019-12-02 04:51:22
(This question follows from this answer ) I am trying to adapt a trampoline function that is currently just passing through a variable number of arguments. I would like to have it convert any argument PyObject* pyob to Object{pyob} , but forward all other arguments through. So (void* self, int, PyObject*, float) -> (int, Object, float) In that example, the first self argument is stripped away. This always happens. Out of the remaining arguments, one of them is of type PyObject*, and hence requires conversion to Object. Here is the function: template <typename T, T t> struct trap; template

why c++ use memset(addr,0,sizeof(T)) to construct a object? Standard or compiler bug?

浪子不回头ぞ 提交于 2019-12-01 18:31:42
问题 This question is related to another post of mine: why allocate_shared and make_shared so slow In here I can describe the question more clearly. Think about the following code: struct A { char data_[0x10000]; }; class C { public: C() : a_() { } A a_; }; int main() { C c; return 0; } I found for the code C() : a_() , the compiler uses memset(addr,0,0x10000) as the constructor of the A. And if the type A has a empty constructor, the asm code is right. To describe the issue more clearly, I wrote

What is the purpose of std::forward()'s rvalue reference overload?

会有一股神秘感。 提交于 2019-12-01 15:04:05
I'm experimenting with Perfect Forwarding and I found that std::forward() needs two overloads: Overload nr. 1: template <typename T> inline T&& forward(typename std::remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); } Overload nr.2: template <typename T> inline T&& forward(typename std::remove_reference<T>::type&& t) noexcept { static_assert(!std::is_lvalue_reference<T>::value, "Can not forward an rvalue as an lvalue."); return static_cast<T&&>(t); } Now a typical scenario for Perfect Forwarding is something like template <typename T> void wrapper(T&& e) { wrapped(forward<T>

What is the purpose of std::forward()'s rvalue reference overload?

偶尔善良 提交于 2019-12-01 13:53:59
问题 I'm experimenting with Perfect Forwarding and I found that std::forward() needs two overloads: Overload nr. 1: template <typename T> inline T&& forward(typename std::remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); } Overload nr.2: template <typename T> inline T&& forward(typename std::remove_reference<T>::type&& t) noexcept { static_assert(!std::is_lvalue_reference<T>::value, "Can not forward an rvalue as an lvalue."); return static_cast<T&&>(t); } Now a typical scenario

how std::thread constructor detects rvalue reference?

假如想象 提交于 2019-12-01 11:16:57
Obviously it is possible to pass an rvalue reference to std::thread constructor. My problem is with definition of this constructor in cppreference . It says that this constructor: template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); Creates new std::thread object and associates it with a thread of execution. First the constructor copies/moves all arguments (both the function object f and all args...) to thread-accessible storage as if by the function: template <class T> typename decay<T>::type decay_copy(T&& v) { return std::forward<T>(v); } As far as I

how std::thread constructor detects rvalue reference?

こ雲淡風輕ζ 提交于 2019-12-01 09:17:59
问题 Obviously it is possible to pass an rvalue reference to std::thread constructor. My problem is with definition of this constructor in cppreference. It says that this constructor: template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); Creates new std::thread object and associates it with a thread of execution. First the constructor copies/moves all arguments (both the function object f and all args...) to thread-accessible storage as if by the function:

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

喜夏-厌秋 提交于 2019-12-01 07:36:39
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 << "is_rvalue reference:" << is_rvalue_reference<T>::value << endl; cout << "is_reference:" << is

Passing variadic template arguments to a variadic function

梦想与她 提交于 2019-12-01 00:18:22
问题 We are using a third-party C library which provides a printf() -style log function, void log(const char *format, ...); For reasons that aren't worth going in to, we need to limit the rate at which messages get logged, something along the lines of void rate_limited_log(const char* format, ...) { if (<not too fast>) { log(format, ...); } } Fortunately the authors of the C library knew what they were doing, and provided void logv(const char* format, va_list ap); so writing the above function is