Type Querying
The delctype operator takes an expression and retrieves its type. In this example, the retrieved type of decltype (myvector.end()) is identical to the return type of myvector.end(). The compiler, not the programmer, traces down the declaration of the particular overloaded vector::end() invoked by myvector.end() and captures its return type. Note that the expression used in a decltype isn't evaluated; the compiler simply looks up its type. Therefore, you can safely use a function call inside a decltypeto capture the return type of that function. The function itself will not not be invoked. You can now write something like this:
//note: pseudo-code
template <class T> decltype(myvector.end())
vector_end(const vector<T>& myvector)
{
return myvector.end();
}
Of course, the line containing the decltype will not compile because the compiler hasn't seen a declaration of myvectorat that point. To solve this problem, C++09 pulls another rabbit from its hat: the return type of a function can be specified after the function's parameter list:
//note: it's almost working
template <class T> vector_end(const vector<T>& myvector) -> decltype(myvector.end()); //return type
The ->after the function's parameter list indicates the return type of that function.
There's one more thing that's missing here, though. To use the new -> notation, the function must be declared auto:
//now it's ready
template <class T>
auto vector_end(const vector<T>& myvector) ->
decltype(myvector.end());
The above declaration reads: vector_end is a function template taking const vector<T>& and returning whatever the return type of myvector.end()is.
With decltype, any modification of the parameter list propagates automatically to the return type:
template <class T>
auto vector_end(vector<T>& myvector) ->
decltype(myvector.end()); //returns vector::iterator
template <class T>
auto vector_end(string& myvector) ->
decltype(myvector.end()); //string::iterator
template <class T>
auto vector_end(const vector<T>& myvector) >
decltype(myvector.end()); //vector::const_iterator
In the examples above, the return type is automatically deduced. However, you can use the ->notation for spelling out an explicit return type as well:
auto func(int n) -> bool;
func() takes a single parameter of type int and returns bool. A more realistic example is a function that returns a pointer to another function:
int handler();
auto get_handler(int signal)-> decltype(handler);
In C++98, the declaration of get_hander()is a monster:
int (*gethandler (int (*pf)()))();
A human programmer would unquestionably prefer to use a typedefinstead:
typedef int(*IF)();
IF gethandler(int signal);
However, the typedef solution is still problematic. The programmer must examine the declaration of handler(), declare a typedef serving as an alias for that type, and modify that typedef whenever handler()changes its signature.
The Golden Trio
The C++09 auto and decltype keywords delegate the task of querying the return type to the compiler. By moving the return type past the parameter list, you can use decltype(whatever-the-return-statement-contains)to deduce the return type automatically.
The new function declaration syntax consists of the following ingredients:
- Declare the function as auto.
- Place -> after the parameter list.
- Add a decltype expression indicating the return type after the ->.
The decltype facility is already used in the range libraryand other C++09 libraries.