Search code examples
c++metaprogramming

Make decltype output a non reference type when called with a pointer


Is there any way, using decltype or any other c++17 feature, to have the following code snippet

T *a = nullptr;
decltype(*a) b = 0;

compile to

T *a = nullptr;
T b = 0;

instead of

T *a = nullptr;
T &b = 0;

Where T is an unknown type.


Solution

  • You can use either std::remove_reference or std::decay like

    int *a = nullptr;
    std::decay_t<decltype(*a)> b = 0;