Skip to content

Instantly share code, notes, and snippets.

@jessoclarence
Created July 29, 2012 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessoclarence/3197239 to your computer and use it in GitHub Desktop.
Save jessoclarence/3197239 to your computer and use it in GitHub Desktop.
Life before templates
#include<iostream>
/** Example is very simple, see LibreOffice code for
real world/complicated examples.
Code is unnecessarily bloated to be clear.
*/
//Macro to create template like functions
#define ADD_FUNC(ret,typedesc) \
typedesc add##ret(typedesc a, typedesc b){\
typedesc c; \
c = a + b; \
return c;\
}
//Normal macro
#define MACRO_ADD(a,b) (a+b)
#define TEN_ADD(a) MACRO_ADD(a,10)
using namespace std;
//Creating our functions
ADD_FUNC(int,int);
ADD_FUNC(float,float);
//Normal template code
template <typename T>
T subtract(T a, T b){
T c;
c = a - b;
return c;
}
int main(){
//Call our macro created functions
cout<<addint(4,5)<<endl;
cout<<addfloat(4.3,5.2)<<endl;
//Call our template function
cout<<subtract(8.4,3.1)<<endl;
cout<<subtract(9,2)<<endl;
//Call normal macro
cout<<MACRO_ADD(1,5)<<endl;
cout<<MACRO_ADD(2.2,3.1)<<endl;
cout<<TEN_ADD(5)<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment