Skip to content

Instantly share code, notes, and snippets.

@FlyingJester
Last active January 7, 2016 01:38
Show Gist options
  • Select an option

  • Save FlyingJester/9d33c17a0e90decc1862 to your computer and use it in GitHub Desktop.

Select an option

Save FlyingJester/9d33c17a0e90decc1862 to your computer and use it in GitHub Desktop.
Demonstration of using using differing templates of a template parameter
#include <functional>
#include <algorithm>
#include <cstdio>
template<template<typename> class Op >
void Test(int &ia, int &ib, float &fa, float &fb, bool first){
if(first){
Op<int> op;
ia = op(ia, ib);
}
else{
Op<float> op;
fa = op(fa, fb);
}
}
int main(){
int ia = 1, ib = 2;
float fa = 5.0f, fb = 9.0f;
printf("> %i, %i, %f, %f\n", ia, ib, fa, fb);
puts( "Expected> 1, 2, 5.0, 9.0");
Test<std::plus>(ia, ib, fa, fb, true);
Test<std::plus>(ia, ib, fa, fb, false);
printf("Plus> %i, %i, %f, %f\n", ia, ib, fa, fb);
puts( "Expected> 3, 2, 14.0, 9.0");
Test<std::multiplies>(ia, ib, fa, fb, true);
Test<std::multiplies>(ia, ib, fa, fb, false);
printf("Multiply> %i, %i, %f, %f\n", ia, ib, fa, fb);
puts( "Expected> 6, 2, 126.0, 9.0");
Test<std::minus>(ia, ib, fa, fb, true);
Test<std::minus>(ia, ib, fa, fb, false);
printf("Subtract> %i, %i, %f, %f\n", ia, ib, fa, fb);
puts( "Expected> 4, 2, 117.0, 9.0");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment