79387350

Date: 2025-01-25 19:14:34
Score: 1
Natty:
Report link

There is a way to distinguish between the different signed, unsigned, and various sized integers. The OP wrote:

  1. I want to know what my options are. It's not that I can't use templates. I want to know whether I have to.
  2. Like I said: I want to be able to call the function both with fixed-size and variable-size integers.

I'm taking your comment to mean you didn't wish to write template functions. While the format appears the same, concepts are not templates and can be used to write functions distinguishing between signed, unsigned, and fixed and variable-sized integers.

In the code, note that char, unsigned char and int8_t are differentiated. Also, see how un/signed short is handled to cover both in one function while excluding any other integers. You can expand the code to cover any cases you wish.

#include <cstdint>

template <class T> //
concept signed_char = std::same_as<char, T>;

void meow(signed_char auto i) {
   println("{}", "signed char");
}

template <class T> //
concept signed_int8_t = std::same_as<int8_t, T>;

void meow(signed_int8_t auto i) {
   println("{}", "signed int8_t");
}

template <class T>                                //
concept unsigned_char =  std::same_as<unsigned char, T>;

void meow(unsigned_char auto i) {
   println("{}", "unsigned char");
}

template <class T>                      //
concept any_short =  std::same_as<short, T> or std::same_as<unsigned short, T>;

void meow(any_short auto i) {
   println("{}", "any short");
}

int main() {
   meow((char)1);
   meow((int8_t)1);
   meow((unsigned char)1);
   meow((short)1);
   meow((unsigned short)1);

#if 0
   // these won't compile
   meow(1);
   meow(1L);
   meow(1z);
#endif
}

Compiled with GCC 12.1 using -std=c++23 -Werror -Wpedantic -Wall

Reasons:
  • Blacklisted phrase (1): I want to know
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Rud48