What are "typedef"s in C++ really used for or necessary for?
#1
Posted 03 January 2012 - 08:19 AM
I know enough to do what I can for now, but I never really understood, even after reading, or saw an overall point as to why, what or how exactly typedefs really do anything useful or are necessary for anything in the first place.
Maybe it's just me unable to understand how a typedef may shape something better, but I seriously see no absolute use in it whatsoever.
#2
Posted 03 January 2012 - 09:44 AM
Apart from that, they're pretty much sugar in c++.
#3
Posted 03 January 2012 - 02:31 PM
#4
Posted 03 January 2012 - 06:03 PM
#5
Posted 04 January 2012 - 07:32 AM
#6
Posted 04 January 2012 - 01:24 PM
std::map<std::vector<std::string>, std::list<std::pair<unsigned long, float> > >
Type that 10 times fast...
#7
Posted 04 January 2012 - 01:57 PM
fireside, on 03 January 2012 - 06:03 PM, said:
Unless your typedef is descriptive.
I do agree it can make code abusers more abusive. It's like giving a switchblade-based thief a gun...
#8
Posted 04 January 2012 - 11:37 PM
There are other practical uses for typedefs in addition to short-hand notations. Here are couple:
1)
typedef unsigned uint32;
2)
define policy classes:
struct my_policy
{
typedef list<int> container;
];
template<class T> void foo(const T &v_)
{
typename T::container c;
...
}
And you can use function pointers without typedefs:
void foo(int) {}
void(*bar)(int)=&foo;
Cheers, Jarkko
#9
Posted 05 January 2012 - 04:39 AM
I now know that a typedef is 100% officially useless now(to me).
#10
Posted 05 January 2012 - 02:47 PM
int may be 8 , 16, 32, 64, 128, or N bits in length, you cannot assume it's 32 bits.
so having a little header file with
#if defined( __64__)
typedef int bigint;
#elif defined(__32__)
typedef long bigint;
#else
#error no idea how big the cpu is
#endif
and then using bigint in all your code is a real lifesaver.
#11
Posted 05 January 2012 - 10:05 PM
fireside, on 03 January 2012 - 06:03 PM, said:
Please take your FUD elsewhere. C++ is not C# or Java, and the C++ type system is exponentially more complex than those other languages. Also, you're just plain wrong. A feature similar to typedefs exists in C#:
using FooList = List<foo>;
Typedefs can actually help make your code more readable, especially when dealing with lots of nested templates. They're not meant to make your code shorter, and if you do use them for that purpose you're just misusing them.
As for whether you actually *need* them, if you work with elaborate template frameworks you actually do. They are used to define type traits (see STL iterators and allocators for example - you can define an allocator that defines a pointer to T to be something other than simply a T*). Or, if you create crossplatform code, a type might be different on each platform, so typedefs are useful there as well.
But I guess you're that wiseass that defines your function returning a pointer to another function as:
int (*MyFuncThatReturnsFuncPtr(int, float)) (const char *); // rather than typedef int MyFuncSignature(const char*); MyFuncSignature* MyFuncThatReturnsFuncPtr(int, float);
Yeah, the first one is way more readable! </sarcasm>
-
Currently working on: the 3D engine for Tomb Raider.
#12
Posted 06 January 2012 - 12:34 AM
Quote
To each his own, I don't see how one is more readable than the other. C# uses delegates which supposedly have some advantages over function pointers.
- Delegates are similar to C++ function pointers, but are type safe.
- Delegates allow methods to be passed as parameters.
- Delegates can be used to define callback methods.
- Delegates can be chained together; for example, multiple methods can be called on a single event.
- Methods don't need to match the delegate signature exactly. For more information, see Covariance and Contravariance
- C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method.
#13
Posted 06 January 2012 - 12:50 AM
-
Currently working on: the 3D engine for Tomb Raider.
#14
Posted 06 January 2012 - 01:20 AM
#15
Posted 06 January 2012 - 05:11 AM
JarkkoL said:
#16
Posted 06 January 2012 - 06:00 AM
#17
Posted 06 January 2012 - 06:57 AM
TheNut, on 06 January 2012 - 05:11 AM, said:
Everybody should just use <cstdint>, then we can all live in peace
One thing I personally use typedefs for, is for my resources. Instead of havig to write Resource<Texture> or Resource<Model> or Resource<etc.>, I typedef them to RTexture and RModel respectively. Less typing = faster coding.
They can also be used to easily attach information to values:
// Unknown unit float getDistance(Position p); // Same type, but now we know the unit typedef float meters; meters getDistance(Position p);
#18
Posted 06 January 2012 - 11:07 AM
In my system int is not a valid data type.
I have KDint64,KDint32, KDint16,KDint8 etc. but no int.
So my alternatives are...
1) Go through every line of code changing the variables
2) Use a global find and replace
3) use typedefs.
No brainer really
#19
Posted 06 January 2012 - 02:42 PM
TheNut, on 06 January 2012 - 05:11 AM, said:
#20
Posted 06 January 2012 - 05:52 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












