perfect-forwarding

Rvalue ref and perfect forwarding

≯℡__Kan透↙ 提交于 2020-01-24 06:01:08
问题 I've read few papers about && and I'm just curious if having: void fnc_1(int&& p) { //... } void fnc(int&& r) { fnc_1(r);//am I suppose to/should I? call it like so:fnc_1(std::forward(r)) } or just passing 'r' is enough? 回答1: fnc_1(r) won't compile, because r is an lvalue, just like any other variable, regardless of type. Yes, that's right, named rvalue references are lvalues, not rvalues. fnc_1(std::forward(r)) also won't compile, because std::forward is specifically designed not to infer

Is this a forwarding reference?

你说的曾经没有我的故事 提交于 2020-01-14 07:06:32
问题 The distinction between rvalue references and forwarding references was made clear enough in this example by Scott Meyers: Widget&& var1 = someWidget; // here, “&&” means rvalue reference (1) auto&& var2 = var1; // here, “&&” does not mean rvalue reference (2) template<typename T> void f(std::vector<T>&& param); // here, “&&” means rvalue reference (3) template<typename T> void f(T&& param); // here, “&&”does not mean rvalue reference (4) Essentially the distinction happens when we have a

No matching function std::forward with lambdas

拟墨画扇 提交于 2020-01-13 10:29:33
问题 Consider the following code, inspired from Barry's answer to this question: // Include #include <tuple> #include <utility> #include <iostream> #include <type_traits> // Generic overload rank template <std::size_t N> struct overload_rank : overload_rank<N - 1> { }; // Default overload rank template <> struct overload_rank<0> { }; // Prepend argument to function template <std::size_t N, class F> auto prepend_overload_rank(F&& f) { using rank = overload_rank<N>; return [f = std::forward<F>(f)]

No matching function std::forward with lambdas

烈酒焚心 提交于 2020-01-13 10:28:11
问题 Consider the following code, inspired from Barry's answer to this question: // Include #include <tuple> #include <utility> #include <iostream> #include <type_traits> // Generic overload rank template <std::size_t N> struct overload_rank : overload_rank<N - 1> { }; // Default overload rank template <> struct overload_rank<0> { }; // Prepend argument to function template <std::size_t N, class F> auto prepend_overload_rank(F&& f) { using rank = overload_rank<N>; return [f = std::forward<F>(f)]

Why move constructor is called twice when passing temporaries to thread function?

元气小坏坏 提交于 2020-01-12 09:18:26
问题 In below code I could not understand why move constructor of class is called twice considering that my thread function is taking argument by rvalue reference and so I was hoping move constructor will be called only once when arguments will be moved to thread constructor.Can somebody give insights on how thread constructor works and how it passes argument to thread function. #include <iostream> #include <thread> #include <chrono> class Test { public: Test() {} Test(Test&&) { std::cout<<"Move

Move semantics and perfect forwarding difference

橙三吉。 提交于 2020-01-10 14:15:13
问题 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? 回答1: 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

How to declare a function template that accepts forwarding reference and returns either a reference or a copy

三世轮回 提交于 2020-01-06 07:26:29
问题 I'm trying to declare a function template that should accept and return a non-const reference when passed an lvalue, but return an RVO-compatible local copy when passed an rvalue: <template ?> ? StringReplace(? str, ? from, ? to); I want template to generate the following signatures: for non-const lvalues std::string& StringReplace(std::string& str, const std::string& from, const std::string& to); for const lvalues std::string StringReplace(const std::string& str, const std::string& from,

Pass by value and move, or two methods [duplicate]

安稳与你 提交于 2020-01-01 11:58:11
问题 This question already has answers here : Why is value taking setter member functions not recommended in Herb Sutter's CppCon 2014 talk (Back to Basics: Modern C++ Style)? (4 answers) Closed 4 years ago . Assume I have the following class, which has a method set_value . Which implementation is better? class S { public: // a set_value method private: Some_type value; }; Pass by value, then move void S::set_value(Some_type value) { this->value = std::move(value); } Define two overloaded methods

Proper style for declaration in range-based for

邮差的信 提交于 2019-12-30 03:05:11
问题 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

SFINAE enable_if for variadic perfect forwarding template on reference/pointer const-ness

ぃ、小莉子 提交于 2019-12-24 03:52:07
问题 I want to create a variadic perfect-forwarding make_shared<T> wrapper, but one which is SFINAEd on whether the constructor of T takes any non-const reference/pointer arguments. The idea is to have two wrappers, called construct and construct_nonconst , where when constructing a Foo(int& r) or Foo(int* r) , one is obliged to use the latter. The purpose of this is so that when developers write classes whose constructors require non-const parameters, they can do so, but it clearly shows up at