Language: C++
A C++0x "multi_cast"
#include <utility> // Note: I've revised my implementation of AUTO_FUN here // to make its use look more like a function definition #define AUTO_FUN_IMPL( ... ) decltype( __VA_ARGS__ ) { return __VA_ARGS__; } #define AUTO_FUN( name_and_param_list ) auto name_and_param_list-> AUTO_FUN_IMPL template< typename SourceType > SourceType&& multi_cast( SourceType&& source ) { return source; } template< typename TargetType , typename... IntermediateTypes , typename SourceType > AUTO_FUN( multi_cast( SourceType&& source ) ) ( static_cast< TargetType > ( multi_cast< IntermediateTypes... > ( std::forward< SourceType >( source ) ) ) ) //////////////////// // Example usage: // //////////////////// struct left {}; struct right {}; struct child : left, right {}; int main() { child child_; left& left_ = child_; right& right_ = multi_cast< right&, child& >( left_ ); }
Tags:
Description:
A C++0x "multi_cast" for issuing a series of static_casts in a row. This is useful for cross-casting or any other static_cast where you need one or more intermediate static_casts.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

