Created
April 11, 2013 23:45
-
-
Save philsquared/5368140 to your computer and use it in GitHub Desktop.
How to write the "left arrow operator" to enable extension methods in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
template<typename T, typename R=void> | |
struct ExtMethod { | |
ExtMethod& operator - () { | |
return *this; | |
} | |
template<typename U> | |
R operator()( U& obj ) { | |
return static_cast<T*>(this)->operator()( obj ); | |
} | |
}; | |
template<typename Derived> | |
struct Extendible | |
{ | |
template<typename T, typename ReturnT> | |
ReturnT operator < ( ExtMethod<T, ReturnT>& extMethod ) { | |
return extMethod( static_cast<Derived&>( *this ) ); | |
} | |
}; | |
struct Widget : Extendible<Widget> { | |
Widget( int size, int weight ) : size( size ), weight( weight ) {} | |
int size; | |
int weight; | |
}; | |
struct print : ExtMethod<print> { | |
void operator()( Widget& w ) { | |
std::cout << "size: " << w.size << ", weight: " << w.weight << std::endl; | |
} | |
}; | |
struct density : ExtMethod<density, float> { | |
float operator()( Widget& w ) { | |
return (float)(w.weight / w.size); | |
} | |
}; | |
int main() { | |
Widget w( 4, 10 ); | |
w<-print(); | |
float d = w<-density(); | |
assert( d - 10/4 < 0.01 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment