[Cs3] Auto and Decltype

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Feb 2 14:54:53 EST 2022


> 
> What is the difference between auto and decltype and where can auto be used
> instead of decltype and vise-versa?

They are similar but the same.

 auto -  infers the type of the variable on the basis of the type of
      	  initializing expression
 decltype() - determines the type of the expression of the parameter. It
 	      is a bit more wordy, since you have to explicitly state
	      the requested type. However, in certain situations,
	      auto may not be used. For example:

	      	         vector<decltype(i)> v;


I have example usage of both auto and decltype() here:

  http://antares.cs.kent.edu/~mikhail/classes/cs3/Examples/Procedural/autoDecltype.cpp

There is an extensive answer to this question is stackoverflow here:

  https://stackoverflow.com/questions/12084040/decltype-vs-auto/12084107

Thanks,
--
Mikhail


More information about the cs3 mailing list