Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid use of typedef?

Tags:

c++

typedef

I have a char (ie. byte) buffer that I'm sending over the network. At some point in the future I might want to switch the buffer to a different type like unsigned char or short. I've been thinking about doing something like this:

typedef char bufferElementType;

And whenever I do anything with a buffer element I declare it as bufferElementType rather than char. That way I could switch to another type by changing this typedef (of course it wouldn't be that simple, but it would at least be easy to identify the places that need to be modified... there'll be a bufferElementType nearby).

Is this a valid / good use of typedef? Is it not worth the trouble? Is it going to give me a headache at some point in the future? Is it going to make maintainance programmers hate me?

I've read through When Should I Use Typedef In C++, but no one really covered this.

like image 732
drby Avatar asked Feb 24 '09 11:02

drby


3 Answers

It is a great (and normal) usage. You have to be careful, though, that, for example, the type you select meet the same signed/unsigned criteria, or that they respond similarly to operators. Then it would be easier to change the type afterwards.

Another option is to use templates to avoid fixing the type till the moment you're compiling. A class that is defined as:

template <typename CharType>
class Whatever
{
   CharType aChar;
   ...
};

is able to work with any char type you select, while it responds to all the operators in the same way.

like image 74
Diego Sevilla Avatar answered Nov 10 '22 11:11

Diego Sevilla


Another advantage of typedefs is that, if used wisely, they can increase readability. As a really dumb example, a Meter and a Degree can both be doubles, but you'd like to differentiate between them. Using a typedef is onc quick & easy solution to make errors more visible.

Note: a more robust solution to the above example would have been to create different types for a meter and a degree. Thus, the compiler can enforce things itself. This requires a bit of work, which doesn't always pay off, however. Using typedefs is a quick & easy way to make errors visible, as described in the article linked above.

like image 33
Gilad Naor Avatar answered Nov 10 '22 10:11

Gilad Naor


Yes, this is the perfect usage for typedef, at least in C.

For C++ it may be argued that templates are a better idea (as Diego Sevilla has suggested) but they have their drawbacks. (Extra work if everything using the data type is not already wrapped in a few classes, slower compilation times and a more complex source file structure, etc.)

It also makes sense to combine the two approaches, that is, give a typedef name to a template parameter.

Note that as you're sending data over a network, char and other integer types may not be interchangeable (e.g. due to endian-ness). In that case, using a templated class with specialized functions might make more sense. (send<char> sends the byte, send<short> converts it to network byte order first)

Yet another solution would be to create a "BufferElementType" class with helper methods (convertToNetworkOrderBytes()) but I'll bet that would be an overkill for you.

like image 40
aib Avatar answered Nov 10 '22 10:11

aib