int versus long

Programming, for all ages and all languages.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

As i said it is just a small enoyance.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

Perhaps you misunderstood the :evil: smiley. I meant I am mad at me when I start <my favorite emulator> and see a char popping up instead of an number. And I am pretty much aware of the fact that C++ had to be backwards compatible with C and am not mad at C++ for that. And I am aware that stdint.h is not standard C++ as of 2003, but iirc it will be in C++0x. I think I also read somewhere or saw it somewhere on google video that there will be a way to do strong typedefs, but I am not very sure about this...
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Solar wrote:The only place I could think of where the "char-ness" of uint8_t actually makes a difference is exactly your cout example. And let's face it, if your application is primitive enough (not using "primitive" to be derogatory) to use cout for output, you can be expected to do those couple of casts instead of turning half the language inside out to "fix" what is a minor nuisance.
That was my point, because uint8_t is defined as

Code: Select all

typedef unsigned char uint8_t;
The operator for "<<" in cout takes a char argument (among others) - guess what uint8_t translates into?

I must ask though, what is the context for using such a small type, why not just use a 32-bit integer (unless size is important, in which case I think cout is not the way forward).

I really must stop replying to this thread....
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

pcmattman wrote:I must ask though, what is the context for using such a small type, why not just use a 32-bit integer (unless size is important, in which case I think cout is not the way forward).
I believe i have allready answered that question, but basicly im a perfectionist. Yes its true, the use of cout is not the best way of doing things, but i am somewhat inexperienced.

Again i do not NEED an 8 bit integer type, it is just curious that it doesnt exist. Again i know that char is an integer type, but it still acts different, and it just seems wrong!

I really should allso stop replying...
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Zacariaz wrote: Again i do not NEED an 8 bit integer type, it is just curious that it doesnt exist. Again i know that char is an integer type, but it still acts different, and it just seems wrong!
This is my last one (I hope)...

OK, the type does not act differently, the cout class uses it differently. The type is nothing more than a way of saying what type of data at the memory address.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

Yes i figured that out by now, but thats not the point. I dont supose it would be hard to get the cout class to treat fx. uint8_t as an 8 bit ingeter instead of a 8 bit char? Why not have both?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

bluecode wrote:And I am aware that stdint.h is not standard C++ as of 2003, but iirc it will be in C++0x.
No, cstdint is the standard header for that purpose, with all the contents in namespace STD as they should be.
I think I also read somewhere or saw it somewhere on google video that there will be a way to do strong typedefs, but I am not very sure about this...

Code: Select all

template <class T>
class StrongTypedef {
  private:
    T _val;
  public:
    StrongTypedef(const T &val) { _val = val; }
    const T &value() { return _val; }
    // other operators as required, probably lots
};

typedef StrongTypedef<uint8_t> byte;
[/quote]

Your optimizer will remove this class and just use it as a typedef. I think if you add a back conversion operator it'll break down (since you can ad-hoc convert back & forth, so it won't bother doing the right thing). You can make the constructor explicit but it makes it a bit more tedious.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Zacariaz wrote:Yes i figured that out by now, but thats not the point. I dont supose it would be hard to get the cout class to treat fx. uint8_t as an 8 bit ingeter instead of a 8 bit char? Why not have both?
Because the typedef of uint8_t translates into unsigned char... It's like doing this:

Code: Select all

#define uint8_t (unsigned char) // the same as typedef unsigned char uint8_t
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Zacariaz wrote:Again i know that char is an integer type, but it still acts different, and it just seems wrong!
Again: It's not "char" that behaves different, it's cout. Because in 99% of the cases you cout a 8-bit-value, you want the character printed, not the value, so that this is the default behaviour.
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Solar wrote:
Zacariaz wrote:Again i know that char is an integer type, but it still acts different, and it just seems wrong!
Again: It's not "char" that behaves different, it's cout. Because in 99% of the cases you cout a 8-bit-value, you want the character printed, not the value, so that this is the default behaviour.
You should be able to add overloads to cout for unsigned and signed chars, as the default char is neither and the three types should be treated as separate (for purposes of name mangling and overloading). I do expect unsigned char to degenerate to char.
Post Reply