-
September 6th, 2013, 10:29 AM
#1
Static & Template sanity check.
Can people just do a quick sanity check of this concept?
I'm playing around with a technique to parameterise a template with the largest 'sizeof'
result for a set of classes.
A, B, C & D are classes with 1, 2, 3 & 4 members respectively.
e is an instance of E, who's array member is sized according to the largest size of A, B, C or D.
Can anyone think of a way that I may fall foul of the 'Static Initialisation Fiasco' with this setup?
Code:
//*********************************************************
// MaxSizeOf for four parameters.
template <const size_t S1,
const size_t S2,
const size_t S3 = 0,
const size_t S4 = 0>
struct MaxSizeOf
{
static const size_t MAX = MaxSizeOf<MaxSizeOf<S1, S2, S3>::MAX, S4>::MAX;
};
// MaxSizeOf for three parameters.
template <const size_t S1,
const size_t S2,
const size_t S3>
struct MaxSizeOf<S1, S2, S3>
{
static const size_t MAX = MaxSizeOf<MaxSizeOf<S1, S2>::MAX, S3>::MAX;
};
// MaxSizeOf for two parameters.
template <const size_t S1,
const size_t S2>
struct MaxSizeOf<S1, S2>
{
static const size_t MAX = (S1 > S2) ? S1 : S2;
};
//*********************************************************
// Define an A.
struct A
{
int i;
};
// Define a B.
struct B
{
int i;
int j;
};
// Define a C.
struct C
{
int i;
int j;
int k;
};
// Define a D.
struct D
{
int i;
int j;
int k;
int l;
};
// Define a templated E.
template <const size_t SIZE>
struct E
{
char a[SIZE];
};
//*********************************************************
// Declare an instance of an E.
E<MaxSizeOf<sizeof(A), sizeof(B), sizeof(C), sizeof(D)>::MAX> e;
//*********************************************************
int main()
{
size_t size = sizeof(e.a);
return 0;
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|