Created
January 13, 2011 15:39
-
-
Save engie/778062 to your computer and use it in GitHub Desktop.
C++ doesn't support template specialisation based on typedefs, so wrap a float in a struct.
This file contains hidden or 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 <iostream> | |
using namespace std; | |
struct Float | |
{ | |
Float( float val ) : m_val( val ) {} | |
operator float() { return m_val; } | |
float m_val; | |
}; | |
#define DeclareFloatType( name ) \ | |
struct name : public Float \ | |
{ \ | |
name( float val ) : Float( val ) {} \ | |
} | |
DeclareFloatType( ReactivePower ); | |
DeclareFloatType( RealPower ); | |
ReactivePower raps[10] = {1,2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
RealPower rps[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
template <class T> | |
float getPower( T* items ) | |
{ | |
return 0; | |
} | |
template<> | |
float getPower( RealPower* items ) | |
{ | |
float val = 0; | |
for( int i = 0; i < 10; i++ ) | |
{ | |
val += items[i]; | |
} | |
return val; | |
} | |
template<> | |
float getPower( ReactivePower* items ) | |
{ | |
float val = 0; | |
for( int i = 0; i < 10; i++ ) | |
{ | |
val += 2 * items[i]; | |
} | |
return val; | |
} | |
int main( int argc, char **argv ) | |
{ | |
cout << getPower( rps ) << " " << getPower( raps ) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment