Decltype dereferenced pointer in C ++

Can someone explain to me why I cannot do something according to:

int* b = new int(5);
int* c = new decltype(*b)(5);

cout << *c << endl;

This calls C464 'int &': cannot use 'new' to highlight the link. How will I do something like this? What I need is the scattered base type of the variable I'm sending.

It works though

int* b = new int(5);
int** a = new int*(b);

decltype(*a) c = *a;
cout << *c<< endl;

I understand how the code above works, but how can I do something like this with a new one?

+4
source share
1 answer

The dereference operator *returns a link that you cannot select with new. Instead, you can use std::remove_pointerin<type_traits>

int* b = new int(5);
int* c = new std::remove_pointer<decltype(b)>::type(5);    
std::cout << *c << std::endl;
+8
source

Source: https://habr.com/ru/post/1619559/


All Articles