This file contains 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> | |
#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