advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 3/5 | Rate this item | 2 users have rated this item.
Clean Up Function Syntax Mess with decltype (cont'd)
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 decltype to 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();
}
advertisement
Of course, the line containing the decltype will not compile because the compiler hasn't seen a declaration of myvector at 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 typedef instead:

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 library and other C++09 libraries.

Previous Page: The Point of No return  
Danny Kalev is a certified system analyst and software engineer specializing in C++ and the theoretical aspects of formal languages. He is the author of Informit C++ Reference Guide and The ANSI/ISO Professional C++ Programmer's Handbook. He was a member of the C++ standards committee between 1997 and 2000. Danny recently finished his MA in general linguistics summa cum laude. In his spare time he likes to listen to classical music, read Victorian literature, and explore natural languages such as Hittite, Basque, and Irish Gaelic. Additional interests include archeology and geology. He also gives lectures about programming languages and applied linguistics at academic institutes.
Page 1: IntroductionPage 3: Type Querying
Page 2: The Point of No return 
Please rate this item (5=best)
 1  2  3  4  5
advertisement