perfect-forwarding

How to perfectly forward `auto&&` in a generic lambda?

允我心安 提交于 2021-02-15 10:16:34
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to

How to perfectly forward `auto&&` in a generic lambda?

不羁岁月 提交于 2021-02-15 10:16:25
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to

C++17: Wrapping callable using generic variadic lambda

蓝咒 提交于 2021-02-07 14:48:13
问题 I want to wrap a callable of any type (e.g. a lambda) transparently inside another callable to inject additional functionality. The wrapper's type should have the same characteristics as the original callable: Identical parameter types Identical return type Perfect forwarding of passed arguments Same behaviour when used in SFINAE constructs I attempted to use generic variadic lambdas as wrappers: #include <iostream> #include <type_traits> template<class TCallable> auto wrap(TCallable&&

C++17: Wrapping callable using generic variadic lambda

随声附和 提交于 2021-02-07 14:45:43
问题 I want to wrap a callable of any type (e.g. a lambda) transparently inside another callable to inject additional functionality. The wrapper's type should have the same characteristics as the original callable: Identical parameter types Identical return type Perfect forwarding of passed arguments Same behaviour when used in SFINAE constructs I attempted to use generic variadic lambdas as wrappers: #include <iostream> #include <type_traits> template<class TCallable> auto wrap(TCallable&&

Is this a universal reference? Does std::forward make sense here?

让人想犯罪 __ 提交于 2021-02-07 05:20:20
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

蓝咒 提交于 2021-02-07 05:20:14
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

Is this a universal reference? Does std::forward make sense here?

二次信任 提交于 2021-02-07 05:18:43
问题 Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple , for example: template <typename T> struct foo { std::decay_t<T> v_; foo(T&& v) : v_(std::forward<T>(v)) {} }; template <typename U> foo<U> make_foo(U&& v) { return { std::forward<U>(v) }; } In the context of Scott Meyers' "universal references", the argument to make_foo is a

How do I forward the values of tuple to a member initializer?

半城伤御伤魂 提交于 2021-01-27 16:28:04
问题 I need to forward the values of a tuple to a member initializer: struct Struct { Member1 member1; Member2 member2; template<typename Tuple1, typename Tuple2> Struct( Tuple1&& tuple1, Tuple2&& tuple2 ) : member1(tuple1...), member2(tuple2...) {} }; The code above obviously isn't valid. How can I express it? Member1 and Member2 have no default/copy/move constructor. I know about std::apply , as suggested in How do I expand a tuple into variadic template function's arguments?. I also know about

How to flatten heterogeneous lists (aka tuples of tuples of …)

牧云@^-^@ 提交于 2021-01-27 04:07:10
问题 I am attempting to employ C++17 fold expressions and the C++14 indices trick to flatten an arbitrary input consisting of tuples and non-tuples. The expected result should at least conform to these requirements: constexpr auto bare = 42; constexpr auto single = std::tuple{bare}; constexpr auto nested_simple = std::tuple{single}; constexpr auto multiple = std::tuple{bare, bare}; constexpr auto nested_multiple = std::tuple{multiple}; constexpr auto multiply_nested = std::tuple{multiple, multiple

How to flatten heterogeneous lists (aka tuples of tuples of …)

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 04:06:51
问题 I am attempting to employ C++17 fold expressions and the C++14 indices trick to flatten an arbitrary input consisting of tuples and non-tuples. The expected result should at least conform to these requirements: constexpr auto bare = 42; constexpr auto single = std::tuple{bare}; constexpr auto nested_simple = std::tuple{single}; constexpr auto multiple = std::tuple{bare, bare}; constexpr auto nested_multiple = std::tuple{multiple}; constexpr auto multiply_nested = std::tuple{multiple, multiple