Skip to content

Instantly share code, notes, and snippets.

Created August 17, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/acd21e856d57760b0976357a88a4d4a2 to your computer and use it in GitHub Desktop.
Save anonymous/acd21e856d57760b0976357a88a4d4a2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cassert>
constexpr int currency(const char *cc)
{
return cc[0] | (cc[1] << 8) | (cc[2] << 16);
}
template<int CUR>
struct Money {
double amount;
Money(double amount) : amount(amount) {}
Money<CUR> operator+(Money<CUR> other) {
return Money<CUR>(this->amount + other.amount);
}
bool operator==(Money<CUR> other) {
return this->amount == other.amount;
}
};
int main(int,char**) {
Money<currency("USD")> five_bucks(5);
Money<currency("USD")> handful_of_dollars(4);
Money<currency("EUR")> some_euros(3);
assert(five_bucks + handful_of_dollars == Money<currency("USD")>(9));
// error: no match for 'operator+' (operand types are 'Money<4477781>' and 'Money<5395781>')
// assert(five_bucks + some_euros == Money<currency("USD")>(8));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment