2

To implement a basic interface for an LCD I need to write the function print for all basic types. I need to write

void print(char c);

and also

void print(uint8_t c);

The first function tells: "I want to write the character c", and the second tells "I want to write the number c". These 2 functions have different meanings, so I need both of them.

The problem is that uint8_t is a typedef of char. If I call

print(20u);

the compiler gives an error because it doesn't know which version of print to choose.

My question is: How to solve this problem?

My solutions:

  1. Define 2 differents functions:
void print(char c);
void print_number(uint8_t x);
void print(uint16_t x);
...

The problem with this solution is that you have to remember that when you want to print an uint8_t you have to call print_number. This complicate all the generic code that I need to write. I don't think is 'the solution'.

  1. Write my proper class uint8_t and instead of using the standard uint8_t use my own version. This solution has different problems:

    • I don't think is a good idea to have an alternative to the standard type uint8_t.
    • If I write my own version of uint8_t, for instance, Uint8_t for consistency I have to write all the other types also (Uint16_t, Uint32_t, ...) and I don't like that too.
  2. ??? Any ideas?

Thanks.

Antonio
  • 579
  • 1
  • 3
  • 12
  • `The problem is that uint8_t is a typedef of char` it's not. It's an alias of `unsigned char` – eerorika Mar 13 '22 at 10:08
  • Does it help to distinguish `signed` and `unsigned char` explicitly? IIRC it's implementation defined how that's handled. Anyway I believe it's better design, to be explicit via function names, rather than generic parameter types for that case. – πάντα ῥεῖ Mar 13 '22 at 10:09
  • 3
    Turn your `print` into a template function, then use `if constexpr` to detect the type of `T` and choose the appropriate print method. – 康桓瑋 Mar 13 '22 at 10:11
  • `the compiler gives an error because it doesn't know which version of print to choose.` do you know which version to use? Cant you guide the compiler at call time? – Pedro Boechat Mar 13 '22 at 10:21
  • 2
    `20u` is considered to be a `unsigned int` though and neither an `uint8_t` nor a `char`. – fabian Mar 13 '22 at 10:22
  • @fabian you are right. Thats the problem. Thank you very much for your help. – Antonio Mar 13 '22 at 10:29

1 Answers1

0

Ok so this might not be the solution you're looking for, but if you type cast while passing the parameter, it works.
print((char) 20u) will call the print(char c)
and
print((uint8_t) 20u) will call the print(uint8_t c)

Batman
  • 196
  • 1
  • 8
  • Advising c-like casts is a little... borderline [ES,4.9](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast). There are also `char` literals and even *user-defined literals*. – MatG Mar 13 '22 at 13:32
  • The underlying issue in the question is that `char` and `uint8_t` are **the same type**. Casting to `char` doesn't change that. If you tested it and it worked, it's because your compiler makes them different types. – Pete Becker Mar 13 '22 at 15:49