5

typedef seems to be C++ specific (perhaps in some other languages). I find it hides the real data type of the value, thus is confusing someone new to the project. Perhaps it is useful to shorten the typing sometimes. When do you recommend the use of typedef?

I also notice I can't pass a typedef-ed value to a function that accepts the underlying type?

something like:

typedef string VAL;
VAL s = "x";
func(string x); // if I try to pass `s`, I get something like no instance of function template match argument list
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 4
    "I also notice I can't pass a typedef-ed value to a function that accepts the underlying type? " that is wrong, you must have some other problem. – juanchopanza Nov 04 '12 at 14:29
  • Please post an [SSCCE](http://sscce.org/) describing your problem. – Some programmer dude Nov 04 '12 at 14:31
  • For example, [this](http://liveworkspace.org/code/0efe500cba72041b9c2ba559e95b1f51) works. – Mankarse Nov 04 '12 at 14:32
  • 2
    What's that "string" in function call? Anyway, one of possible duplicates: http://stackoverflow.com/q/4541345/1202636 (a rapid search here or on Google shows a lot of informations) – effeffe Nov 04 '12 at 14:37
  • `typedef` comes from C, and is related to the fact that C (and, by extension, C++) maintains two slightly different symbol tables (or at least two different categories of symbols within a single table). `typedef` is sometimes needed to "bridge" between the two. But I've never managed to figure out the details, or find a good description of the distinction, so I just find an example and copy it when the need arises. – Hot Licks Nov 04 '12 at 14:43
  • @juanchopanza, is right, I made a mistake elsewhere – Jiew Meng Nov 04 '12 at 15:07

4 Answers4

4

typedef is a C and C++ language label used to create a new type from an existing type. The best place is to use it with the C programming language with structs such as the following example or to use it to create a new, more abstract type from an existing primitive type (short, long, etc.).

typedef struct {
    int iValue;
    int jValue;
} MyValues;
// declare a variable with this struct
MyValues aMyValuesStruct;

Before typedef was introduced into C, in order to use the above struct as a type you would have had to do something like the following:

struct _MyValuesStruct {
    int iValue;
    int jValue;
};
// declare a variable with this struct
struct _MyValuesStruct aMyValuesStruct;

In C++ structs and classes are similar constructs which create new types so the typedef is not as useful for those types of variables. However if you are using a built in primitive type to represent some other, more abstract type then the typedef statement helps you to specify the type in a more abstract way.

This use of typedef makes it a lot easier to specify a label for a type so that when a programmer is reading the source code they can understand what a particular variable represents. For instance if you have a color value variable that fits in an unsigned short if you use a typedef to create a new type for color value and use it consistently then it is much easier to recognize that a variable contains a color value and not some other kind of an unsigned short.

What typedef also allows is that it makes it much easier to find all of the places in the source where a particular abstract type is being used and it makes it easier to change the underlying type. For instance if your color value type starts off as a byte so you use typedef to create an abstract type like typedef unsigned char ColorValue; and then later you find that you need to change it from an unsigned char to an unsigned short, there would be a single place to make the change and if your use of the type was reasonably strict, that would be the only place you need to make a change.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
1

Perhaps it is useful to shorten the typing sometimes. When do you recommend the use of typedef?

One good usecase is when you want to be able to remap a type without editing your codebase:

typedef int32_t pixel_t;

Another one is where you want the type to be more explicit so that the usage of the code becomes clearer:

typedef int distance_t;

typedef int angular_momentum_t;
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
1

Your question is answered in that other question.

typedefs are useful (in C as well as C++, and perhaps Objective-C too?) to abstract implementation details. It is good practice to hide the type to parts of a program which don't need to know how the type is implemented, so as to avoid polluting that code with problems unrelated to its function, and make it easier to change the type implementation without having to change that code.

Someone new to the project might find it actually easier to cope with the code if unnecessary details are hidden where possible.

As for your problem, func(string x); is a function prototype declaration, not a function call.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52
0

typedef is used to hide the underlying type representing the data, in some circumstances.

For example, the Standard leaves it unspecified what the typical size of a short int, int or long int are, which might be an issue. On the other hand, the implementation should (now) provide int8_t, int16_t, int32_t and int64_t (and possibly int128_t depending on the platform) so you can have integers with a known range of values at your disposition.

But what is int32_t ? Well, it's a signed integer, with a width of 32 bits. It could be any of char, short int, int, long int or long long int in theory... and which it is should not matter: it is an implementation detail.

Therefore, at some point in your standard library headers you'll see:

typedef int int32_t; // most likely representation

and this definition is conditioned on the platform.

In your code, using int32_t let you leave behind those dreadful platform peculiarities and concentrate on the business at hand: your business.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722