Help with typedef

F

felixnielsen

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

This is not a problem without at solution, however i am no certain of
where the solution lies.

2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Maybe typedef isnt the way to go, but what is the alternatives?

Thanks in advance.

Zacariaz
 
B

Ben Pope

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;

That doesn't define a new type, it aliases an existing type.
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

So what, you would like a compile time diagnostic? Something at run
time? An editor that prevents you from typing more 'f's?

It's also platform / compiler dependant - an unsigned short does not
have to be 16bits.
This is not a problem without at solution, however i am no certain of
where the solution lies.

Well, I'm not sure what the problem is.
2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Not with that syntax.
Maybe typedef isnt the way to go, but what is the alternatives?

You could use your own type (a struct or class), but I'm not sure what
problem you're trying to solve.

I *think* you want integer types that are of a given size (in bits).
There is no standard way of doing it in C++.

Take a look at boost:
http://www.boost.org/libs/integer/integer.htm
specifically, the header:
http://www.boost.org/boost/cstdint.hpp

And also at the C99 header, "stdint.h", which may also be found as
<cstdint> with your library. (or may not).

Neither of these are C++ standard, but something approximating either
are very likely to be, in the future.

Ben Pope
 
V

Victor Bazarov

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

This is not a problem without at solution, however i am no certain of
where the solution lies.

There is none. The truncation is done by the compiler at the time of
initialisation. You're explicitly allowed to initialise an unsigned short
with an expression that produces an unsigned int or unsigned long.
2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Maybe typedef isnt the way to go, but what is the alternatives?

If your system has eight-bit bytes, you should be able to say

typedef unsigned char U8;

But if it doesn't, there is no way. A minimal addressable element of
your computer memory is a byte ('char'), and you cannot declare an object
of any size smaller than that stand-alone.

V
 
F

felixnielsen

Ben Pope skrev:
That doesn't define a new type, it aliases an existing type.


So what, you would like a compile time diagnostic? Something at run
time? An editor that prevents you from typing more 'f's?

It's also platform / compiler dependant - an unsigned short does not
have to be 16bits.


Well, I'm not sure what the problem is.


Not with that syntax.


You could use your own type (a struct or class), but I'm not sure what
problem you're trying to solve.

I *think* you want integer types that are of a given size (in bits).
There is no standard way of doing it in C++.

Take a look at boost:
http://www.boost.org/libs/integer/integer.htm
specifically, the header:
http://www.boost.org/boost/cstdint.hpp

And also at the C99 header, "stdint.h", which may also be found as
<cstdint> with your library. (or may not).

Neither of these are C++ standard, but something approximating either
are very likely to be, in the future.

Ben Pope

To say it as simple as posible.

I need four type:

unsigned char
unsigned short
unsigned long
unsigned long long

First of all i would like to redefine their names as to how many bit
they occupy and wether they are signed or not:

unsigned char - U8
unsigned short - U16
unsigned long - U32
unsigned long long - U64

signed char - S8
signed short - S16
signed long - S32
signed long long - S64

or something like it.

Second, the unsigned char i really enoying.
Yes its true, you dont have to store a letter in a char, but if you
store numbers, every time you need to pull out the number again.
std::cout << static_cast<int>(char_var);
is the way to do it, if im not mistaken.

As i said, it is a problem with a solution, but i dont much like the
solution.
 
F

felixnielsen

Victor Bazarov skrev:
There is none. The truncation is done by the compiler at the time of
initialisation. You're explicitly allowed to initialise an unsigned short
with an expression that produces an unsigned int or unsigned long.


If your system has eight-bit bytes, you should be able to say

typedef unsigned char U8;

But if it doesn't, there is no way. A minimal addressable element of
your computer memory is a byte ('char'), and you cannot declare an object
of any size smaller than that stand-alone.

V
As writen in the answer to ben hope, the char type, is enoying as hell
(excuse me my big mouth)
std::cout << static_cast<int>(char_var);
Would be the correct way to pull a value from a char type, this is
enoying and i would rather i didnt have to do it that way.
 
V

Victor Bazarov

[...]
To say it as simple as posible.

I need four type:

unsigned char
unsigned short
unsigned long
unsigned long long

No such type in C++. As much as you may need it, it simply isn't there.
First of all i would like to redefine their names as to how many bit
they occupy and wether they are signed or not:

unsigned char - U8
unsigned short - U16
unsigned long - U32
unsigned long long - U64

signed char - S8
signed short - S16
signed long - S32
signed long long - S64

or something like it.

Second, the unsigned char i really enoying.

Uh... I don't understand that statement.
Yes its true, you dont have to store a letter in a char, but if you
store numbers, every time you need to pull out the number again.
std::cout << static_cast<int>(char_var);
is the way to do it, if im not mistaken.

But you are mistaken. Just use that value in an expression. You seem to
be solving a wrong problem.
As i said, it is a problem with a solution, but i dont much like the
solution.

Again, I don't understand. What exactly don't you like about what
solution?

V
 
V

Victor Bazarov

[..]
As writen in the answer to ben hope

"Ben Pope", actually.
>, the char type, is enoying as hell
(excuse me my big mouth)
std::cout << static_cast<int>(char_var);
Would be the correct way to pull a value from a char type, this is
enoying and i would rather i didnt have to do it that way.

First of all, to "pull a value", you don't need to print it. Just use
a variable of that type in an expression. Yes, if you want to _print_
it, you need to use the right conversion, and outputting a char (whether
signed or unsigned) is a special action in C++, and you need to cast, as
you do. What's the big deal?

V
 
T

TB

(e-mail address removed) sade:
Victor Bazarov skrev:

As writen in the answer to ben hope, the char type, is enoying as hell
(excuse me my big mouth)
std::cout << static_cast<int>(char_var);
Would be the correct way to pull a value from a char type, this is
enoying and i would rather i didnt have to do it that way.

std::cout << int(char_var);
 
D

Daniel T.

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

This is not a problem without at solution, however i am no certain of
where the solution lies.

2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Maybe typedef isnt the way to go, but what is the alternatives?

Thanks in advance.

Zacariaz

Run this:

int main()
{
cout << "sizeof( unsigned char ) == "
<< sizeof( unsigned char ) << '\n';
cout << "sizeof( unsigned short ) == "
<< sizeof( unsigned short ) << '\n';
cout << "sizeof( unsigned int ) == "
<< sizeof( unsigned int ) << '\n';
cout << "sizeof( unsigned long ) == "
<< sizeof( unsigned long ) << '\n';
}

The one who's sizeof is 1 can hold 0xff, sizeof 2 can hold 0xffff,
sizeof 4 can hold 0xffffffff and so on...
 
F

felixnielsen

Well, i didnt quite get the answer i was looking for, however i realize
now that the problem maybe isnt at great as i thought.
Thank you all a bunch.

Regards
 
V

Victor Bazarov

Daniel said:
[..]
Run this:

int main()
{
cout << "sizeof( unsigned char ) == "
<< sizeof( unsigned char ) << '\n';

'sizeof(unsigned char)' is _always_ 1.
cout << "sizeof( unsigned short ) == "
<< sizeof( unsigned short ) << '\n';
cout << "sizeof( unsigned int ) == "
<< sizeof( unsigned int ) << '\n';
cout << "sizeof( unsigned long ) == "
<< sizeof( unsigned long ) << '\n';
}

The one who's sizeof is 1 can hold 0xff, sizeof 2 can hold 0xffff,
sizeof 4 can hold 0xffffffff and so on...

This is misleading. Did you mean to imply that 'sizeof' somehow gives
the number of bits in the type/object? That's not true by itself. The
'sizeof' operator is only useful in determining the number of bits in
the object/type _iff_ it's combined with CHAR_BIT value (multiplied by
it, anyway):

cout << "number of bits in an unsigned char is " << CHAR_BIT << endl;
cout << "number of bits in an unsigned short is " <<
sizeof(unsigned short)*CHAR_BIT << endl;

and so on.

V
 
B

Ben Pope

Ben Pope skrev:

To say it as simple as posible.

I need four type:

unsigned char
unsigned short
unsigned long
unsigned long long

First of all i would like to redefine their names as to how many bit
they occupy and wether they are signed or not:

unsigned char - U8
unsigned short - U16
unsigned long - U32
unsigned long long - U64

OK, this is platform and compiler dependant. Your platform might not
even have an 8 bit char.

long long doesn't exist in C++. It's is compiler dependant.
signed char - S8
signed short - S16
signed long - S32
signed long long - S64

Similarly as above.
or something like it.

Second, the unsigned char i really enoying.
Yes its true, you dont have to store a letter in a char, but if you
store numbers, every time you need to pull out the number again.
std::cout << static_cast<int>(char_var);
is the way to do it, if im not mistaken.

I'm not sure what you mean by "pull out", but if you want to format it
as a number, not a character, you need to make it a number. It's not
called a "char" for no reason; the usual type to put in it is a
character. The << operator is for formatting, hence, it is formatted as
a character.
As i said, it is a problem with a solution, but i dont much like the
solution.

I don't know of any suitable alternative. You may not like it, but it
is explicit as to what you want to achieve, and is also simple.


#include <iostream>

// these two functions are explicit, but not sure what
// they gain you in real terms, make maintenance harder
unsigned int as_integer(unsigned char c) {
return static_cast<unsigned int>(c);
}

signed int as_integer(signed char c) {
return static_cast<signed int>(c);
}

// this class is just obscure, don't see the point, really.
class my_U8 {
public:
my_U8() : c_(0) {}
my_U8(unsigned char c) : c_(c) {}
// this should be chosen in most cases
operator unsigned char&() { return c_; }
// this one seems to be chosen for the stream (dunno why)
operator unsigned int() { return static_cast<unsigned int>(c_); }
private:
unsigned char c_;
};


int main() {
unsigned char c = 'A';
std::cout << "uchar: " << c
<< "\nuint: " << as_integer(c) << std::endl;

my_U8 u = 'B';
std::cout << "my_U8: " << u << std::endl;

u += 3;

std::cout << "my_U8: " << u << std::endl;
}

Not recommended though, just use the static cast to make it explicit.

Ben Pope
 
F

felixnielsen

Wether this is smart or not i found a "solution"

typedef struct {
unsigned a:8;
} uint8;
uint8 test;
test.a = 7;
std::cout << test.a;

Now i just need to know how to get rit of the'a', because i dont really
need it.
 
V

Victor Bazarov

Wether this is smart or not i found a "solution"

typedef struct {
unsigned a:8;
} uint8;
uint8 test;
test.a = 7;
std::cout << test.a;

Now i just need to know how to get rit of the'a', because i dont really
need it.

Why do you think it's a solution? What was the problem you were trying
to solve?

struct uint8 {
unsigned a:8;
uint8& operator =(unsigned u) { a = u & 0xff; return *this; }
operator int() const { return a; }
};

#include <iostream>
int main() {
uint8 test;
test = 7;
std::cout << test << " occupies " << sizeof(test) << " bytes\n";
}

Now, run it and see if you "solved" anything.

V
 
B

Bo Persson

Wether this is smart or not i found a "solution"

typedef struct {
unsigned a:8;
} uint8;
uint8 test;
test.a = 7;
std::cout << test.a;

Now i just need to know how to get rit of the'a', because i dont
really
need it.

This isn't really a solution, as the struct will be the size of an
unsigned int, even if you only use 8 bits of it.


Bo Persson
 
R

Rolf Magnus

As writen in the answer to ben hope, the char type, is enoying as hell
(excuse me my big mouth)
std::cout << static_cast<int>(char_var);
Would be the correct way to pull a value from a char type, this is
enoying and i would rather i didnt have to do it that way.

This has nothing to do with "pulling out" a value. char is just an integer
type like the others. What you are talking about is a property of the
ostream class, which has an operator<< that interprets char values as
characters. If that was done different, how would you print a single
character?
 
D

Default User

What i really wanna do, is defining my own types, it doesnt really
matter why.


Actually, it probably DOES matter why. As the others have explained,
much of what you want to do isn't possible or isn't easy. It would be
better to tell us what problem you are trying to solve. There may be
other solutions besides fixed-width data types.




Brian
 
F

felixnielsen

The short answer is:
Im trying to learn.
When i ask a question like this, i would be so much easyer if you would
either say that there is no solution, witch ofcourse wrong, as there,
in my experience allways is, a solution, or simply refrain from
answering or say that you dont know the answer ect.

however, i do actually have a problem witch need a solution.

i need to store alot of different values from 0 -> 0xffffffff eg. they
can be contained in a unsigned long/unsigned int, however, a single
vector cant contan them as the limit to how many values it can contain
is something like 0x4000000 and i need to store more.
What i need is at class/function that can take 2 or more vector and
treath them as one plus initiate a new one if nesacery.
I would indeed be wery thankfull if anyone could help me with that,
otherwise i consider this subject closed.

Once again thank you all for your time.
 
R

Rolf Magnus

The short answer is:
Im trying to learn.
When i ask a question like this, i would be so much easyer if you would
either say that there is no solution, witch ofcourse wrong, as there,
in my experience allways is, a solution, or simply refrain from
answering or say that you dont know the answer ect.

however, i do actually have a problem witch need a solution.

i need to store alot of different values from 0 -> 0xffffffff eg. they
can be contained in a unsigned long/unsigned int,

An unsigned int might not be enough then, because depending on the compiler,
it can be as small as 16 bits.
however, a single vector cant contan them as the limit to how many values
it can contain is something like 0x4000000 and i need to store more.

How did you determine that value? Through the max_size() member function? Or
by just trying? Do you have enough RAM for this? Does your compiler support
single memory blocks that big? 0x4000000 values would take 256MB of memory.
Depending on how you add new values (if you use push_back() without a
previous reserve()), it will probably need a lot more memory. It could
easily be three times as much temporarily.
What i need is at class/function that can take 2 or more vector and
treath them as one plus initiate a new one if nesacery.

I fail to see what that has to do with your typedef question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,697
Members
45,488
Latest member
MohammedHa

Latest Threads

Top