2

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?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Why have two different structures for the same kind of data? Why not store the amount (`value`) and the currency (Euro or Dollar) in a single structure? – Some programmer dude Dec 21 '21 at 12:14
  • 2
    As for your problem you might want to read about [user-defined literals](https://en.cppreference.com/w/cpp/language/user_literal). Not sure if the Euro or Dollar symbols are valid though. Perhaps `3_euro` and `6_dollar` instead? – Some programmer dude Dec 21 '21 at 12:15
  • @Someprogrammerdude because I want to overload functions based on the type of currency – 463035818_is_not_an_ai Dec 21 '21 at 12:15
  • 2
    Whereas `$` is in ascii range, `€` is not, so it would be more problematic... – Jarod42 Dec 21 '21 at 12:15
  • 1
    Won't something like this work? `void someFunc( dollar&& amount );` and then `someFunc( dollar(3) );` – digito_evo Dec 21 '21 at 12:22
  • @Jarod42 I think it's the other way around, actually -- as a "regular" Unicode symbol, the Euro symbol is accepted as a standard universal character, but the dollar symbol is not. – Sneftel Dec 21 '21 at 12:27
  • U+20AC "Euro" is a valid identifier character. U+0024 "Dollar Sign" is not a valid identifier character. U+1F4B2 "Heavy Dollar Sign" is a valid identifier character. – Eljay Dec 21 '21 at 13:23
  • @Eljay actually most compilers accept `$` as a valid character in identifiers as an extension: [dollar sign in variable name?](https://stackoverflow.com/q/7926394/995714). But not all compilers deal well with Unicode characters in identifiers – phuclv Dec 21 '21 at 13:41
  • @phuclv • All the C++ compilers I use either accept `$` as an identifier character, or if a compiler flag is provided will accept `$`. (And, I admit, I use `$` in some of my identifiers, in particular `FOO$` is a macro, and `$BAR(x)` is a function-like macro. I only use `$` in macros. I hate macros.) But it's not standard C++. – Eljay Dec 21 '21 at 15:08

2 Answers2

4

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';
}
Michal
  • 148
  • 2
  • 6
3

You can get close to this syntax with user defined literals : Note not all compilers accept special characters but my MSVC does. Ofcourse you could make one currency class and do some kind of conversion based on exchange rates but that's up to you :)

#include <iostream>

struct dollars_t
{
    double value;
};

struct rupees_t
{
    double value;
};

constexpr dollars_t operator "" _$(long double value)
{
    return dollars_t{ static_cast<double>(value) };
};

constexpr rupees_t operator "" _₹(long double value)
{
    return rupees_t{ static_cast<double>(value) };
};


int main()
{
    auto dollars = 10.12_$;
    auto rupees = 1000.12_₹;

    std::cout << "You have " << dollars.value << " dollars\n";
    std::cout << "And you have " << rupees.value << " rupees\n";

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19