get the type to which a pointer points

How to retrieve/get the type to which a pointer points on compile time like t illustrated in:

1
2
3
4
5
6
7
int *p;
//..
//...

t =  p ; // ..? how make variable t is "int" and can be used as:

str = new t[512] ; // ?? 
Last edited on
auto q = new decltype(*p)[512];
Last edited on
The value category of the expression *p is lvalue.

const auto q = new std::remove_reference< decltype(*p) >::type[512]{};
Last edited on
^ Yes, thank you.
The compiler sees the code and so do you. While type info is available, do you really need it?

How about:
1
2
3
4
5
6
7
8
using myInt = int; // the type is defined in one place

myInt *p;

myInt *str = new myInt[512]{};
delete [] str; str = nullptr;
// or better yet:
std::vector<myInt> str( 512 );
Last edited on
Topic archived. No new replies allowed.