CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C++

A C++0x "multi_cast"

476 Views
Copy Code Show/Hide Line Numbers
#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_ );
}
by Matt Calabrese
  January 27, 2010 @ 9:55pm
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.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate