3 ms·
If you had to Google what `std::ref` is then you aren't a very proficient C++ developer - but if you had seen the actual example rather than the stripped down o
by clappski 7y ago
If you had to Google what `std::ref` is then you aren't a very proficient C++ developer - but if you had seen the actual example rather than the stripped down one in the GitHub repo, you would probably understand straightaway - https://www.boost.org/doc/libs/1_70_0/libs/histogram/doc/html/histogram/getting_started.html https://www.boost.org/doc/libs/1_70_0/libs/histogram/doc/htm...
- jzwinck 7y ago> If you had to Google what `std::ref` is then you aren't a very proficient C++ developer I can't recall the last time I saw someone use std::ref. Maybe once in the past 5 years, and I think I deleted it after I fixed something else. One of the good things about C++ is that you don't have to use all of it (in fact, you probably should not use all of it).
- clappski 7y agoIt's very useful when you want to pass something by reference into a function that takes a varadic template parameters and uses perfect forwarding, i.e. https://godbolt.org/z/xlmwFw https://godbolt.org/z/xlmwFw template <typename... Args> void foo( Args&&... args ) { whiz( std::forward<Args>( args )... ); } ... int i = 5; double k = 2.3; foo( std::ref( i ), k ); // take i by ref, k by value I've also seen it a lot with std::thread, and basically anything else that takes a callable/functor where the developer chose not to use lambda's with reference captures. Would agree that it isn't a major language feature, but I would expect competent developers to at least know about it and why you would want to/have to use it (if they had worked with a compiler that had >=C++11 support).
- stochastic_monk 7y agoYou’d need them for std::future/async/thread, which is my primary use case. I agree that not needing to use all of C++ is one of the best things about it. (Opt-in vs opt-out.)