Initialized object solutions

J

JKop

Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

template<class T>
struct ValueInitialized : public T
{
ValueInitialized() : T() {}
};


template<class T>
struct ValueInitialized_WorksWithIntrinsics
{
T object;

ValueInitialized_WorksWithIntrinsics() : object() {};
};



And here's some sample usage code:


template<class T>
void GiveMeAnyType()
{
ValueInitialized<T> t;
}


struct SamplePOD
{
char a;
void* p_cheese;
signed milk;
};

struct NonPODAggregate
{
std::string talk;
int r;
};

int main()
{
GiveMeAnyType<std::eek:stringstream>(); //Has a private copy constructor


GiveMeAnyType<SamplePOD>();

GiveMeAnyType<NonPODAggregate>();

//GiveMeAnyType<int>(); //Opps! Can't inherit from "int"

ValueInitialized_WorksWithIntrinsics<int> rans;

int& jack = rans.object;

ValueInitialized_WorksWithIntrinsics<int> const season;

int const & winter = season.object;

}



Thoughts?


-JKop
 
C

Chris Theis

JKop said:
Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

template<class T>
struct ValueInitialized : public T
{
ValueInitialized() : T() {}
};


template<class T>
struct ValueInitialized_WorksWithIntrinsics
{
T object;

ValueInitialized_WorksWithIntrinsics() : object() {};
};



And here's some sample usage code:


template<class T>
void GiveMeAnyType()
{
ValueInitialized<T> t;
}


struct SamplePOD
{
char a;
void* p_cheese;
signed milk;
};

struct NonPODAggregate
{
std::string talk;
int r;
};

int main()
{
GiveMeAnyType<std::eek:stringstream>(); //Has a private copy constructor


GiveMeAnyType<SamplePOD>();

GiveMeAnyType<NonPODAggregate>();

//GiveMeAnyType<int>(); //Opps! Can't inherit from "int"

ValueInitialized_WorksWithIntrinsics<int> rans;

int& jack = rans.object;

ValueInitialized_WorksWithIntrinsics<int> const season;

int const & winter = season.object;

}



Thoughts?

I´m somehow missing the credit to the people who showed you this ;-)

Cheers
Chris
 
J

JKop

Chris Theis posted:
I´m somehow missing the credit to the people who showed you this ;-)

Cheers
Chris


Well I did mention in the other thread! :p

For clarity:

These ideas came from:


Rob Williscroft
Tobias Güntner


-JKop
 
H

Howard

JKop said:
Thoughts?


-JKop

I'm still confused as to what problem this is all supposed to solve.
Specifically, why, if you need your objects initialized, do you not have
constructors for them to initialize them as you desire? And for things like
local integer variables (etc.), you can always initialize them at
declaration time, right? I just don't see where I'd ever actually *need* to
use one of those templates. Perhaps I'm missing some special case where
neither of the approaches I've mentioned works? (Or perhaps you just enjoy
solving problems, whether they ever come up or not? I know I did, before I
had to work all day. :))

-Howard
 
J

JKop

Howard posted:
I'm still confused as to what problem this is all supposed to solve.
Specifically, why, if you need your objects initialized, do you not
have constructors for them to initialize them as you desire? And for
things like local integer variables (etc.), you can always initialize
them at declaration time, right? I just don't see where I'd ever
actually *need* to use one of those templates. Perhaps I'm missing
some special case where neither of the approaches I've mentioned works?
(Or perhaps you just enjoy solving problems, whether they ever come up
or not? I know I did, before I had to work all day. :))

-Howard


I work... just not a programming - I ain't got that piece of paper that
says I can programme!


-JKop
 
I

Ioannis Vranos

JKop said:
Two templates. The first is more convenient, but you can't use it with
intrinsics. The second is less convenient, but it can be used with *all*
types. Here's the templates:

Thoughts?


As I said recently in the other thread, the initial problem does not
make sense, and thus any solution to it doesn't make sense either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top