Deduced types can often be seen using IDE editors, compiler error messages, and the Boost TypeIndex library, but the results of some tools may be neither helpful nor accurate.
Dependending on the phase of the software development process, we might get type deduction information during coding, compilation, and runtime.
IDE Editors
Code editors in IDEs often show the types of program entities when we hover our cursor over the entity. In order for the IDE to offer this kind of information, our code must be in a more or less compilable state. Moreover, when more complicated types are involved, the information displayed by IDEs may not be helpful.
Compiler Diagnostics
We can get a compiler to show what it deduced for a type by causing a compilation problem using that type:
|
|
|
|
For the code above, since there’s no template definition to instantiate, compiler will yield the error messages like this:
|
|
Through these errors, we get useful type information.
Runtime Output
Consider a more complex example involving a user-defined type (Widget
), an STL container(std::vector
), and an auto
variable (vm
), which is more representative of the situation where we want to see deduced type information:
|
|
This time, if we want to see what type is deduced for T
and param
, the type information displayed by IDE editors is not reliably useful. For example, the deduced type for T
is shown as:
|
|
and the param
’s type is:
|
|
In order to create a textual representation of the type we care about and print it out on screen, we might consider the Boost TypeIndex library1. For example:
|
|
Under compilers from GNU and Clang, Boost.TypeIndex produces this accurate output:
|
|
-
There is something similar called
typeid
andstd::type_info::name
in Standard C++ that could also display the type, but the output text may not be straightforward (for example, using “PK” to stand for “pointer to const”) and might not be reliable. ↩︎