T
Tomás
Let's write a header file which has all sorts of things in it which help a
programmer overcome what some people may see as "warts in C++", or maybe
things that are just more convenient or nicer to work with. For instance, if
you want to default initialise something, you have to know what kind of type
you're dealing with.
A) Intrinsic
int a = 0;
char* p = 0;
long r(0);
B) Array or POD Structure
SomeStructure blah = {};
int monkey[5] = {}
C) A class (i.e. a non-POD)
SomeClass blah;
However let's say you're writing a template function that hasn't got a clue
what it's working with... it needs a universal way of default initialising
something. The following class comes in handy:
template<class T>
class DefaultInitialised
{
public:
DefaultInitialised() : T() {}
};
typedef DefaultInitialised DefaultInitialized; //Keep the Americans happy!
Now here's our template function:
template<class T>
void Chocolate()
{
DefaultInitialised<T> object;
// do some stuff
}
But still we need to make room for intrinsics:
template<class T>
class DefaultInitialisedBearer
{
public:
T object;
DefaultInitialisedBearer() : object() {}
};
Now our template function could look like so:
template<class T>
void Chocolate()
{
DefaultInitialisedBearer<T> bearer;
T &object = bearer.object;
// do some stuff
}
I frequently find myself typing out such classes, when it'd be better for me
to just make a header file and throw it into the "standard include files
folder".
One gem I came across recently was posted by Ben; he gave a nice way of
working with what some people may think are arkward types. (I imagine that
some people here are quick to jump up and say that these types are only
arkward because a particular programmer isn't proficient enough to work with
it, but what they hey...).
You can return an array by reference as follows:
char ( &GetArray() )[6]
{
static char hello[] = "hello";
return hello;
}
An alternative was posted by Ben:
(Slightly modified by me)
template <class T>
class TypeSheath
{
typedef T actual;
typedef T& reference;
typedef T* pointer;
typedef const T* pointer_to_const;
};
TypeSheath<char[6]>::reference GetArray()
{
static char hello[] = "hello";
return hello;
}
While it's open opinion which is "better", I quite like the latter version.
So, anyone got any other ideas for what else to stick in this header file?
I'm going to put a little help file with it to explain the merits of each
little gem.
-Tomás
programmer overcome what some people may see as "warts in C++", or maybe
things that are just more convenient or nicer to work with. For instance, if
you want to default initialise something, you have to know what kind of type
you're dealing with.
A) Intrinsic
int a = 0;
char* p = 0;
long r(0);
B) Array or POD Structure
SomeStructure blah = {};
int monkey[5] = {}
C) A class (i.e. a non-POD)
SomeClass blah;
However let's say you're writing a template function that hasn't got a clue
what it's working with... it needs a universal way of default initialising
something. The following class comes in handy:
template<class T>
class DefaultInitialised
{
public:
DefaultInitialised() : T() {}
};
typedef DefaultInitialised DefaultInitialized; //Keep the Americans happy!
Now here's our template function:
template<class T>
void Chocolate()
{
DefaultInitialised<T> object;
// do some stuff
}
But still we need to make room for intrinsics:
template<class T>
class DefaultInitialisedBearer
{
public:
T object;
DefaultInitialisedBearer() : object() {}
};
Now our template function could look like so:
template<class T>
void Chocolate()
{
DefaultInitialisedBearer<T> bearer;
T &object = bearer.object;
// do some stuff
}
I frequently find myself typing out such classes, when it'd be better for me
to just make a header file and throw it into the "standard include files
folder".
One gem I came across recently was posted by Ben; he gave a nice way of
working with what some people may think are arkward types. (I imagine that
some people here are quick to jump up and say that these types are only
arkward because a particular programmer isn't proficient enough to work with
it, but what they hey...).
You can return an array by reference as follows:
char ( &GetArray() )[6]
{
static char hello[] = "hello";
return hello;
}
An alternative was posted by Ben:
(Slightly modified by me)
template <class T>
class TypeSheath
{
typedef T actual;
typedef T& reference;
typedef T* pointer;
typedef const T* pointer_to_const;
};
TypeSheath<char[6]>::reference GetArray()
{
static char hello[] = "hello";
return hello;
}
While it's open opinion which is "better", I quite like the latter version.
So, anyone got any other ideas for what else to stick in this header file?
I'm going to put a little help file with it to explain the merits of each
little gem.
-Tomás