3 ms·
std::forward (and reference collapsing rules, which are related) is imo one of the most complicated parts of C++. Generally, the idea is that it's there to impl
by foota 3y ago
std::forward (and reference collapsing rules, which are related) is imo one of the most complicated parts of C++. Generally, the idea is that it's there to implement "perfect forwarding". Essentially, being able to accept and pass to another function any type of parameter without coercing it to something like a const reference, which can be inefficient, or limit the way a caller can use your function.
I'm not trying to gate keep, but if you don't understand unique pointers and templates in general, you're a long way from needing or being able to make use of perfect forwarding imo. You don't need perfect forwarding outside of generic wrapper type code, and in most code you can just accept a specific type of object (e.g., value types or const references).
If you want to understand it you should start with learning value categories, move semantics, and templates, and then try to learn perfect forwarding.
- samsquire 3y agoThe bit between the < and > I find hard to understand. In C++ behaviours are named, like in some C++ code coroutines there is a template that looks like: template <is_task task_t> The is_task looks like an if statement.
- foota 3y agoMy impression is that coroutines aren't super stable today, but what you're seeing there is a C++20 "concept", essentially, a way to restrict the types accepted by a template. E.g., the is_task part is a concept that requires the template parameter type task_t to meet some criteria. Normal template code looks like: template <typename T> Where you can also choose to use the class keyword instead of typename. C++20 isn't very widely used though, so this isn't something you're likely to run into in anything other than cutting edge code. If you want you can read about them here: https://en.cppreference.com/w/cpp/language/constraints https://en.cppreference.com/w/cpp/language/constraints but you're probably better not using concepts at least at first. In general, https://en.cppreference.com/ https://en.cppreference.com/ is a very high quality source for learning, although it's very dry as well.