0

I need to be able to differentiate between data types which have the same sizes but different signed-ness properties. An example follows,

template <size_t N>
struct alias;

template<>
struct alias<sizeof(unsigned char)>{
    using Type = unsigned char;
};

template<>
struct alias<sizeof(signed char)>{
    using Type = signed char;
};

using uint8 = alias<1>::Type;
using int8 = alias<1>::Type;  //This is supposed to be signed type

Since first specialization uses unsigned char and both signed and unsigned chars use the same size, alias<1>::Typeresults returning unsigned char. But I want to be able to return same data type as both signed and unsigned.

I am also aware there are std::is_signed or std::is_unsigned under type_traits header file. I could not figure out how to use them in this context.

EDIT

Partial Complete Code

#define BYTE 1

template <size_t N>
struct alias;

#define REGISTER_ALIAS(X)           \
template <>                         \
struct alias <sizeof(X)> {          \
           using Type = X;          \
};

REGISTER_ALIAS(unsigned char)
REGISTER_ALIAS(signed char)

using int8 = alias<BYTE>::Type;
using uint8 = alias<BYTE>::Type;
nmd_07
  • 666
  • 1
  • 9
  • 25
  • What are you ultimately trying to do with this `alias` struct? What's the context you're planning on using it in? – qxz Feb 19 '17 at 21:29
  • Creating fix-width data types without hardcoded typedefs. – nmd_07 Feb 19 '17 at 21:31
  • 3
    Sounds like you just want another template parameter. – Barry Feb 19 '17 at 21:33
  • I actually tried using another template parameter but could not get it to work in this context. I may have missed something. – nmd_07 Feb 19 '17 at 21:34
  • 1
    Can you post the code you tried, and why it didn't work as you wanted? – qxz Feb 19 '17 at 21:34
  • It actually works, only thing left to accomplish is just to be able to differentiate between signed and unsigned types. – nmd_07 Feb 19 '17 at 21:35
  • 1
    I've already implemented what youre trying to do. I added an enum template parameter for signness – Indiana Kernick Feb 19 '17 at 21:38
  • @Kerndog73 Could you provide some examples as to how you used enum template parameter to accomplish the task? I have shared part of my code now. – nmd_07 Feb 19 '17 at 21:40

2 Answers2

1

This is what i did.

enum Sign {
  SIGNED,
  UNSIGNED
};

template <size_t SIZE, Sign SIGN>
struct Alias;

template <>
struct Alias<1, SIGNED> {
  using type = int8_t;
};
//etc
Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
  • The problem with this solution is it still requires hardcoding. I want everything to be deduced by the compiler including signed-ness. You first mark the template to be SIGNED by hand and alias signed value. I want the given value to be marked as signed or unsigned by the compiler. – nmd_07 Feb 19 '17 at 21:52
0

I don't understand what do you want.

But I suppose that you have to explicit, in some way, that you want a signed type or an unsigned type.

I propose the following way, based on a couple of different types and an explicit selection

template <size_t N>
struct alias;

template<>
struct alias<sizeof(char)>{
    using sType = signed char;
    using uType = unsigned char;
};

using uint8 = alias<1>::uType;
using int8 = alias<1>::sType;
max66
  • 65,235
  • 10
  • 71
  • 111
  • For example, I can specialize the alias struct with both unsigned and signed chars without any problem during compilation. Assuming char is only 1 byte, calling alias<1>::Type returns whatever alias specialization for char data type is defined first although there are two specializations. I wanna be able to use both signed and unsigned chars specialization. Basically there are two specializations that corresponds to 1. Is there a way for me to use both? – nmd_07 Feb 19 '17 at 22:42
  • @nomadov - I don't know how; how can the compiler understand when using the signed version and when the unsigned one? – max66 Feb 19 '17 at 22:45