0

When I run the snippet below, it prints out:

int&
int __cdecl(void) 

I was expecting the second line to just be int

Why is this happening? What could I do to fix it if it were inside a templated function that takes pointers or iterators so I couldn't use std::remove_pointer.

#include <type_traits>
#include <iostream>
int main()
{
    int r = 4;
    int* rp = &r;
    using return_type = decltype(*rp);
    using no_ref_type = std::remove_reference<return_type>::type();
    std::cout << typeid(return_type).name() << '&' << std::endl;
    std::cout << typeid(no_ref_type).name();
}
JadeSpy
  • 140
  • 1
  • 8
  • 6
    Because you added parentheses after the type in `std::remove_reference::type();` - it should be just `std::remove_reference::type;` – UnholySheep Nov 16 '21 at 17:16
  • For the second print, the issue is [Most Vexing Parse.](https://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) – jxh Nov 16 '21 at 17:19
  • @jxh -- that's not the [most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse). It's simply a function declaration, just like `int f();`. – Pete Becker Nov 16 '21 at 17:59
  • 1
    @PeteBecker Yes, it perfectly looks like a function declaration if you know what you are looking at (except there isn't a function name). It also looks like invoking a static method named `type`, but that is not what it is doing. – jxh Nov 16 '21 at 18:04
  • @jxh -- yes, it's something that's commonly misunderstood. But the "most vexing parse" is more subtle. – Pete Becker Nov 16 '21 at 18:07
  • @PeteBecker I understand your point, but anytime someone expected a constructor or method invocation but instead gets a function declaration is basically a most vexing issue caused by misunderstanding how to parse the syntax. – jxh Nov 16 '21 at 18:14
  • @jxh -- "vexing" and "most vexing" are not the same thing. – Pete Becker Nov 16 '21 at 18:15
  • Side note, most of the type-generating templates in the type_trait library expose `XXX_t`, which can be used in place of `XXX::type`. So in your case, it would be `remove_reference_t`. – Ranoiaetep Nov 16 '21 at 18:29

0 Answers0