Giuseppe: writes:
Quote:
? And then maybe I could concatenate the two size_t and cast them into
that uint64_t? Would a uint64_t be enough? I was thinking about this:
observe(uint64_t* wordid);
and then
uint64_t chain(uint32_t one, uint32_t two) {
// get two 32bit rands and concatenate
uint64_t longrand = one;
longrand <<= 32;
longrand |= two;
return longrand;
}
|
Well, that might work, but what's going to happen is that a few years from
now your code will be the featured article on
http://www.thedailywtf.com,
and everyone in the world will be laughing at it.
If you have a requirement that you must, somehow, process either a uint64_t
or a pair of size_t's, then you do exactly that, instead of trying to invent
some rogue mutation:
bool observe(uint64_t* wordid)
{
// …
}
bool observe(size_t word1, size_t word2)
{
// …
}
and factor out the common logic into a separate set of classes or functions,
that get invoked by the overloaded function code. Overloading, in C++, is
there precisely to address this situation.
There are many ways other to do this that are clean, and the resulting code
is readable and maintainable. Another way, that comes to mind, for example:
struct observe_params {
uint64_t *wordptr;
size_t word1;
size_t word2;
};
bool observe(uint64_t *wordptr)
{
struct observe_params parms;
parms.wordptr=wordptr;
parms.word1=0;
parms.word2=0;
return observe(parms);
}
bool observe(size_t word1, size_t word2)
{
struct observe_params parms;
parms.wordptr=NULL;
parms.word1=word1;
parms.word2=word2;
return observe(parms);
}
bool observe(struct observe_parms &parms)
{
// …
}
Then you supply the required logic here. If the wordptr class member is not
null (presuming that observe(int64_t *) never receives a null ptr), your
code knows that it received a uint64_t ptr. If the wordptr class member is
null, your code uses the two size_t parameters.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAkh4vocACgkQx9p3GYHlUOJaDgCfSvtDUjJWCV nlQPKevp+F5vKv
l28Ani5mdNrO4SoZTtLACReeHG/Q4wA/
=digB
-----END PGP SIGNATURE-----