perfect-forwarding

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

那年仲夏 提交于 2019-12-18 11:46:47
问题 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:

How useful would Inheriting Constructors be in C++?

半城伤御伤魂 提交于 2019-12-17 17:57:19
问题 As I sit in the C++ Standards committee meetings, they are discussing the pros and cons of dropping Inheriting Constructors since no compiler vendor has implemented it yet (the sense being users haven't been asking for it). Let me quickly remind everyone what inheriting constructors are: struct B { B(int); }; struct D : B { using B::B; }; Some vendors are proposing that with r-value references and variadic templates (perfect forwarding constructors), it would be trivial to provide a

Perfect forwarding - what's it all about? [duplicate]

醉酒当歌 提交于 2019-12-17 15:22:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Advantages of using forward Could someone please explain to me what perfect forwarding is about? 回答1: http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html Why is this useful? Well, it means that a function template can pass its arguments through to another function whilst retaining the lvalue/rvalue nature of the function arguments by using std::forward. This is called

Can I typically/always use std::forward instead of std::move?

孤人 提交于 2019-12-17 06:24:08
问题 I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. Meyers says that he does not care about the answer because it is non-idiomatic and would silly his mind, but I'm still interested. The code presented is as follows: // Typical function bodies with overloading: void doWork(const Widget& param) // copy { //

What's the difference between std::move and std::forward

只谈情不闲聊 提交于 2019-12-17 02:17:08
问题 I saw this here: Move Constructor calling base-class Move Constructor Could someone explain: the difference between std::move and std::forward, preferably with some code examples? How to think about it easily, and when to use which 回答1: std::move takes an object and allows you to treat it as a temporary (an rvalue). Although it isn't a semantic requirement, typically a function accepting a reference to an rvalue will invalidate it. When you see std::move , it indicates that the value of the

make_unique and perfect forwarding

一笑奈何 提交于 2019-12-16 19:58:39
问题 Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3); This hides the new nicely and only mentions the type once. Anyway, here is my attempt at an implementation of make_unique : template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std:

Why forwarding return value is needed

一笑奈何 提交于 2019-12-14 00:19:23
问题 In the doc of std::forward , it gave the following example: template<class T> void wrapper(T&& arg) { foo(forward<decltype(forward<T>(arg).get())>(forward<T>(arg).get())); } Why is forwarding of return value needed here? What's the cases where it is different to the following code: template<class T> void wrapper(T&& arg) { foo(forward<T>(arg).get()); } 回答1: Let's break down the possibilities. T::get could return an lvalue reference (which is an lvalue expression), an rvalue reference (which

How do correctly use a callable passed through forwarding reference?

落爺英雄遲暮 提交于 2019-12-12 17:13:46
问题 I'm used to pass lambda functions (and other callables) to template functions -- and use them -- as follows template <typename F> auto foo (F && f) { // ... auto x = std::forward<F>(f)(/* some arguments */); // ... } I mean: I'm used to pass them through a forwarding reference and call them passing through std::forward . Another Stack Overflow user argue (see comments to this answer) that this, calling the functional two or more time, it's dangerous because it's semantically invalid and

Linker error (undefined reference) with `static constexpr const char*` and perfect-forwarding [duplicate]

£可爱£侵袭症+ 提交于 2019-12-12 16:36:00
问题 This question already has answers here : Undefined reference to static constexpr char[] (6 answers) Closed 4 years ago . #include <iostream> using namespace std; template<typename T> void print(T&& mX) { std::cout << std::forward<T>(mX) << std::endl; } struct SomeStruct { static constexpr const char* someString{"hello!"}; SomeStruct() { print(someString); } }; int main() { SomeStruct s{}; return 0; } clang++ -std=c++1y ./code.cpp -o code.o /tmp/code-a049fe.o: In function `SomeStruct:

Why can't I add a layer of abstraction with type lookup to strip references in C++?

懵懂的女人 提交于 2019-12-11 15:49:40
问题 This is kind of a followup question to Why can't I use traits with forwarding references in C++? which has been correctly answered. However I tried my own solution below https://godbolt.org/z/X7dBz1 #include <type_traits> template <typename T, typename enable = void> struct Traits { static const bool value = false; }; template <typename T> struct Traits<T,std::enable_if<std::is_reference<T>::value>> { static const bool value = Traits<typename std::remove_reference<T>::type>::value; }; struct