Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1287

Answer by HolyBlackCat for Passing a vector of pointers as an argument on the fly (C++)

$
0
0

Rather than having the wrapper forward arguments to the constructor (adding unnecessary moves in some cases even if done using perfect forwarding; and just not looking good to my taste), I'd do this:

template <typename T>[[nodiscard]] T &unmove(T &&value){    return static_cast<T &>(value);}
Foo(std::vector<Base *>{&unmove(Derived1(1)), &unmove(Derived2("name"))});

Such unmove() function lets you take addresses of temporaries, and is useful in some other cases too (e.g. the last usecase I remember is passing rvalues to std::make_format_args()).


Viewing all articles
Browse latest Browse all 1287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>