I want to write some code that uses different types of currencies, eg
struct euro {
int value;
};
struct dollar {
int value;
};
Now I'd like to use the euro and dollars sign in code, something like
euro e = 3€;
dollar d = 3$;
Is this possible somehow?
What you need is user defined lietrals. The code below worked for me using g++ 11.1.0
but I guess that there could be some problems with non ASCII €. Maybe try using EUR
suffix? For negative values see this.
#include <charconv>
#include <cstring>
#include <iostream>
struct euro
{
unsigned long long val;
};
euro operator"" _€ (unsigned long long num)
{
return euro {num};
}
int main()
{
euro e = 123_€;
std::cout << e.val << '\n';
}