I’ve just seen this really nice talk Rock Hard: C++ Evolving by Boris Jabes. In the section of the talk concerning Higher-Order Generic Programming he says that the following is an example of a function that is more generic with regards to its return type and leads to fewer template function overloads
template <typename Func>
auto deduce(const Func & f) -> decltype(f())
{..}
This however can be realized using plain template syntax as follows
template <typename Func>
Func deduce(const Func & f)
{..}
so I guess the example chosen doesn’t really show the unique power of decltype. Can anyone give an example of such a more enlightening usage of decltype?
Your suspicions are incorrect.
Now
deduce(&f)has typevoid, but with your rewrite, it has typevoid(*)(). In any case, everywhere you want to get the type of an expression or declaration, you usedecltype(note the subtle difference in between these two.decltype(x)is not necessarily the same asdecltype((x))).For example, it’s likely your Standard library implementation somewhere contains lines like
Finding out the correct return type of
addhas been a challenging problem throughout past C++. This is now an easy exercise.Little known is that you can use
decltypebefore::and in a pseudo destructor name