473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Gems -- #include <gems.hpp>


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
Mar 4 '06 #1
7 1654
Tomás wrote:
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.


Could you download Boost and read every source file, please?

;-)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 4 '06 #2
Tomás wrote:

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!

This typedef is illegal.
Mar 5 '06 #3
> template<class T>
class DefaultInitialised
{
public:

DefaultInitialised() : T() {}
};
How would the above compile? T is neither a base nor a member you can't
initialize it.

Perhaps you meant:

template <typename T>
struct DefaultInitialized
{
T obj;
T():obj(){}
};

typedef DefaultInitialised DefaultInitialized; //Keep the Americans happy!
From what you have written DefaultInitialised is, as far as i can see,
a *template*, not a class, not a type. And so you can't typedef it. It
should not compile.
Now here's our template function:

template<class T>
void Chocolate()
{
DefaultInitialised<T> object;

// do some stuff
}
In fact, if you want to default *contruct* an object of type T, just write:

T object;
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
}
Ok, I see your point and I think its not a bad idea after all...however
this might be too elaborating. How about:

T object = T();

It looks simpler and it definitely works well (given your compiler has
enough power to optimize away the temporary.)
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.
Thanks for that. Better though, you may like to have a look at the boost
library. It provides a lot of (professionally written and well tested)
constructs that IMO more or less refines the C++ language.

If I am not too mistaken boost might as well be a superset of what you
are proposing.

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

Ben
Mar 5 '06 #4
benben wrote:
template<class T>
class DefaultInitialised
{
public:

DefaultInitialised() : T() {}
};


How would the above compile? T is neither a base nor a member you can't
initialize it.

Perhaps you meant:

template <typename T>
struct DefaultInitialized
{
T obj;
T():obj(){}
};


Now, benben, I would have expected you to have learnt from his mistake:
Always paste from your editor, and attempt to compile the example.
Perhaps _you_ meant:

template <typename T>
struct DefaultInitialized {
T obj;
DefaultInitialized():obj(){}
};

:P

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 5 '06 #5
> Now, benben, I would have expected you to have learnt from his mistake:
Always paste from your editor, and attempt to compile the example.
Perhaps _you_ meant:

template <typename T>
struct DefaultInitialized {
T obj;
DefaultInitialized():obj(){}
};
LOL indeed! What a mistake! My apologies!!!!

:P

Ben Pope

Mar 5 '06 #6
Tomás posted:

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


Okay, I'll have a look a boost... but only because of peer pressure :P

-Tomás
Mar 5 '06 #7
Ok, I see your point and I think its not a bad idea after all...however
this might be too elaborating. How about:

T object = T();

It looks simpler and it definitely works well (given your compiler has
enough power to optimize away the temporary.)

Try it like as follows:

std::ostringstream object = std::ostringstream();

Aren't private copy constructors a bitch!
The idea with "DefaultInitialisedBearer" was that you could default
initialise *anything*, whether it was an intrinsic, an array, a POD, a
non-POD class, or even a class which has a private copy contructor.

-Tomás
Mar 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
1
by: Ingo Nolden | last post by:
Hi, I am using spirit 1.31 I have been trying the following example from the spirit docs. I tried it with int and double neither works: vector<int> v; rule<> r = list_p(int_p, ch_p(',')); ...
4
by: Steven T. Hatton | last post by:
I'm trying to create an baseclass that will serve as a parent for reference counted objects handled by boost::intrusive_ptr<>. The documentation didn't provide much in the way of describing what...
4
by: John | last post by:
I have a function declaration that gives an error while compiling. Can anyone help me figure this one out? inline void create(const std::vector< myclass >& plist, std::vector< myclass...
6
by: Andreas Schmitt | last post by:
Hi, I got a problem here that I haven't been able to fix so far since I don't even know what the problem is. I have the following two files (among one other 'Class1.cpp') in my Project in...
3
by: tkirke | last post by:
How does one transfer a buffer object from python -c and back again (assuming the data gets modified)? I can't seem to get this or anything else to work, but am clueless as to what I'm doing wrong...
1
by: Colin Caughie | last post by:
Is there a general rule/convention for when to use angle brackets and when to use quotes in #include statements? Is the angle bracket reserved for "system" header files (e.g. standard library...
3
by: Chris Jones | last post by:
Hi, I've experimenting with using boost::pool_allocator with std::vector and gcc (4.1)., and I am having problems with segmentation violations. Below I give a simple example of one way I am...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.