Difference between decltype and typeof?

MBZ picture MBZ · Jan 3, 2013 · Viewed 7.2k times · Source

Two question regarding decltype and typeof:

  • Is there any difference between the decltype and typeof operators?
  • Does typeof become obsolete in C++11?

Answer

Grizzly picture Grizzly · Jan 3, 2013

There is no typeof operator in c++. While it is true that such a functionality has been offered by most compilers for quite some time, it has always been a compiler specific language extension. Therefore comparing the behaviour of the two in general doesn't make sense, since the behaviour of typeof (if it even exists) is extremely platform dependent.

Since we now have a standard way of getting the type of a variable/expression, there is really no reason to rely on non portable extensions, so I would say it's pretty much obsolete.

Another thing to consider is that if the behaviour is of typeof isn't compatible with decltype for a given compiler it is possible that the typeof extension won't get much development to encompass new language features in the future (meaning it might simply not work with e.g. lambdas). I don't know whether or not that is currently the case, but it is a distinct possibility.