Simple overloading without complications
Adam Sansier via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 12 14:13:54 PDT 2016
On Tuesday, 12 July 2016 at 17:17:31 UTC, Kagamin wrote:
> On Tuesday, 12 July 2016 at 16:30:05 UTC, Adam Sansier wrote:
>> Doesn't matter, it's not what I asked.
>
> Yeah, I'm not confident I understood your problem right. You
> can try to describe your problem better.
Criteria:
1. At most 2 one parameter functions be used, one that takes an
int and the other a wstring.
2. They must be overloadable and allow for a literal string type
to be passed for the wstring function. This prevents basic
templates and variant techniques.
3. No duplication of code.
4. The wstring function only exists to find the index for the
argument passed. It cannot do this without the int function being
called first, as it initializes or gets data that cannot be done
more than once. This forces some type of "break" in the flow of
the int function.
suppose you have two functions and only two functions.
void Do(int i)
{
// stuff 1
// stuff 2 (uses i)
}
void Do(wstring s)
{
// stuff 1
// converts s to i, the same i that is used in Do(int)
// stuff 2 (uses i)
}
Obviously stuff 1 and stuff 2 are duplicates. stuff 1 does not
not use i or s. stuff 2 does not use s.
The question is how to optimally, in terms of code duplication,
reduce Do(string) so it does no extra work.
There are ways, obviously. But to satisfy the complete criteria
is the hard part.
I could probably use some goto statements and asm to accomplish
this, but probably quite a bit of work and tricky
void Do(wstring s)
{
// get i from s (a simple search), no big deal, a comment
suffices
// create function call to (setup stack, modify first stack
parameter, which is i, to use our new i
goto Doi;
}
void Do(int i)
{
// stuff 1
label Doi;
// stuff 2
}
The goto bypasses stuff1 in Do(int), and sets up i with the new
value. This method would work but is a hack, not portable, etc.
It is essentially the same idea as the yield I proposed, but less
robust. A yield and continue construct would hide all the details
and do something similar.
Again, this isn't about how to accomplish some goal, but how to
accomplish it given the criteria/restraints proposed. It's a no
brainier to do without the constraints.
More information about the Digitalmars-d-learn
mailing list