How can I convert a reference type to a value type?

c++, c++11, decltype, reference

Solution

You can use `std::remove_reference` to make it a non-reference type:

std::numeric_limits<
    std::remove_reference<decltype(*p)>::type
>::max();

Live demo

or:

std::numeric_limits<
    std::remove_reference_t<decltype(*p)>
>::max();

for something slightly less verbose.

Problem

I'm trying to move some code to templates using the new `decltype` keyword, but when used with dereferenced pointers, it produces reference type. SSCCE: ``` #include <iostream> int main() { int a = 42; int *p = &a; std::cout << std::numeric_limits<decltype(a)>::max() << '\n'; std::cout << std::numeric_limits<decltype(*p)>::max() << '\n'; } ``` The first `numeric_limits` works, but the second throws a `value-initialization of reference type 'int&'` compile error. How do I get a value type from a pointer to that type?

Original source