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: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
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: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