if you’re declaring variables inside a class. Instead of typing out the full
name of the type of the iterator, you can use decltype:, you can’t use auto.
This works because decltype doesn’t actually execute the expression given as
its argumentâĂŤit is only used by the type checker to determine a type.
We can also use decltype, along with auto to make determining a function’s
return type easier. Suppose that we had a function which took a template
argument that is a container, and we were to return an iterator into that
container, we could write:
We could have also done the above example with declval. The template class
declval creates a default constructed type of its argument for use in type
checking. However, since it can only be used in unevaluated expressions, and
therefore is not evaluated, it doesn’t actually create a default constructed
object, the type doesn’t even need a default constructor, and in fact, it can
be used with an incomplete type. Typically it is used with decltype, since
decltype doesn’t evaluate its argument. The above example could be
rewritten as: