Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the type of a pointer

Tags:

c++

c++11

I want to do something like this:

SomeType *y;

/* ... snip ... */

auto x = new decltype(y); // Create a new pointer "x" to a SomeType object.

But decltype(y) is SomeType* and decltype(*y) is SomeType&. Is there a way to get plain SomeType out of y?

like image 771
Jake Avatar asked Jul 04 '15 23:07

Jake


People also ask

What is the type of a pointer?

There are majorly four types of pointers, they are: Null Pointer. Void Pointer. Wild Pointer.

What is pointer and its types in C++?

In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.

Can a pointer be any type?

Every pointer is of some specific type. There's a special generic pointer type void* that can point to any object type, but you have to convert a void* to some specific pointer type before you can dereference it.

Why do we have different types of pointers?

There are several reasons: Not all addresses are created equal; in particular, in non Von Neuman (e.g. Harvard) architectures pointers to code memory (where you often store constants) and a pointers to data memory are different.


1 Answers

Since decltype(*y) is a reference, you can use std::remove_reference:

new std::remove_reference<decltype(*y)>::type;
like image 174
Ami Tavory Avatar answered Oct 03 '22 13:10

Ami Tavory