c++ – Allocating memory of type auto

Question:

Why does this line not cause an incident among the developers of the standard?

auto p = new auto(42);

Answer:

There are four types of storage duration for an object, and accordingly there are four ways to create an object:

  • static, static T variable(expression) ,
  • thread, thread_local T variable(expression) ,
  • automatic, T variable(expression) ,
  • dynamic, new T(expression) .

It would be strange if type inference only worked for the first three ways, but not for dynamically allocated memory.

Scroll to Top