Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to create a local variable with the same type as a deduced argument?

Namely:

[](auto const& foo) {
    ??? bar; // should be same base type as foo, minus const&
}

So far, I'm using:

typename std::remove_const<typename std::remove_reference<decltype(foo)>::type>::type combination

But I'm really hoping theres an easier alternative!

like image 263
mmocny Avatar asked Jun 07 '14 03:06

mmocny


1 Answers

std::decay<decltype(whatever)>::type, or decay_t if your std library has been updated with it.

It emulates various kinds of function argument decays. It handles if your arg was a reference-to-function. On reference-to-array, it produces a pointer as well, which is less ideal.

If you want to handle those differently, you'll have to roll your own.

like image 138
Yakk - Adam Nevraumont Avatar answered Oct 13 '22 23:10

Yakk - Adam Nevraumont