int versus long
Perhaps you misunderstood the 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...
-
- 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:
That was my point, because uint8_t is defined asSolar 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.
Code: Select all
typedef unsigned char uint8_t;
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....
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.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).
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...
-
- 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:
This is my last one (I hope)...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!
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.
No, cstdint is the standard header for that purpose, with all the contents in namespace STD as they should be.bluecode wrote: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...
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.
-
- 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:
Because the typedef of uint8_t translates into unsigned char... It's like doing this: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?
Code: Select all
#define uint8_t (unsigned char) // the same as typedef unsigned char uint8_t
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.Zacariaz wrote:Again i know that char is an integer type, but it still acts different, and it just seems wrong!
Every good solution is obvious once you've found it.
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.Solar wrote: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.Zacariaz wrote:Again i know that char is an integer type, but it still acts different, and it just seems wrong!