I'd like to test a property of the type of a variable. I do can it, but the code is too verbose.
Consider an example, in which I define a variable of the same type as the type of a value in a container:
#include <vector>
int main() {
std::vector<int> v, &rv=v;
// ‘rv’ is not a class, namespace, or enumeration
//rv::value_type i1;
// Ok
decltype(v)::value_type i2;
// decltype evaluates to ‘std::vector<int>&’, which is not a class or enumeration type
//decltype(rv)::value_type i3;
// Ok
std::remove_reference<decltype(rv)>::type::value_type i4;
}
I can live with decltype
, but adding std::remove_reference
is too much. Are there any nice way to shorten the code, without defining auxiliary templates?
You can shorten that with one of
std::decay_t<decltype(rv)>::value_type i4 = 42;
or
std::decay_t<decltype(*std::begin(rv))> i4 = 42;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With