Skip to content

Instantly share code, notes, and snippets.

@Gitmoko
Last active August 9, 2016 16:32
Show Gist options
  • Save Gitmoko/79daf9dc4dfd43587ab6fdc0ec24b0cc to your computer and use it in GitHub Desktop.
Save Gitmoko/79daf9dc4dfd43587ab6fdc0ec24b0cc to your computer and use it in GitHub Desktop.
#ifndef BinaryOperatorsCalculatersH
#define BinaryOperatorsCalculatersH
#include<algorithm>
#define ParserOpeq ==
#define ParserOpnoteq !=
#define ParserOpand_ &&
#define ParserOpor_ ||
#define ParserOprelless <
#define ParserOprelmore >
#define ParserOpreleqless <=
#define ParserOpreleqmore >=
#define ParserOpadd +
#define ParserOpsub -
#define ParserOpmul *
#define ParserOpdiv /
#define ParserMakeOp(x) ParserOp##x
#define MakeCalculaters(x)\
template<>\
class Calculaters<MyParser::operators::x >{\
public:\
template<class T1,class T2>\
constexpr static auto Calc(T1 t1, T2 t2) {\
return t1 ParserMakeOp(x) t2;\
}\
}
namespace MyParser {
enum struct operators {
eq, noteq
, and_, or_
, relless, relmore, releqless, releqmore
, add, sub
, mul, div
};
template<MyParser::operators Op>
class Calculaters;
MakeCalculaters(add);
MakeCalculaters(eq);
MakeCalculaters(noteq);
MakeCalculaters(and_);
MakeCalculaters(or_);
MakeCalculaters(relless);
MakeCalculaters(relmore);
MakeCalculaters(releqless);
MakeCalculaters(releqmore);
MakeCalculaters(sub);
MakeCalculaters(mul);
MakeCalculaters(div);
}
struct G{
int a = 0;
constexpr G(int arg = 0):a(arg){}
constexpr G operator+(G arg){
return{a + arg.a};
}
};
static_assert(MyParser::Calculaters<MyParser::operators::add>::Calc(2,3) == 5);
static_assert(MyParser::Calculaters<MyParser::operators::add>::Calc(G{2},G{3}).a == 4);
int main(){}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment