Topic: Named function parameters using anonymous structs for
Author: Matthew Fioravante <fmatthew5876@gmail.com>
Date: Sun, 15 Jun 2014 11:17:24 -0700 (PDT)
Raw View
------=_Part_664_7945367.1402856244939
Content-Type: text/plain; charset=UTF-8
A possible solution to implementing named function parameters.
//declaration
//throws on error
void display(Screen& target, { int xres, int yres/*, etc..*/ }); //<-
compiler generated anonymous struct type
//definition
void display(Screen& target, {} p) { //<- p derives anonymous struct type
from declaration. Error if no declaration present.
doDisplayWork(target, p.xres, p.yres);
}
//At call site
Screen screen;
display(screen, {}); //<-defaults
display(screen); //<-has a default set to {}, works like positional default
args
display(screen, 640, 480);
display(screen, yres=480, xres=640);
display(screen, { yres=480, xres=640});
This might have better support in ABI. The type of the function is derived
from the type of the anonymous struct. This could be exploited to enhance
error checking in the compiler.
A tool in clang could probably be written to convert legacy interfaces on
large C++ code bases to decide how to use the new style on a case by case
basis.
This idea could be implemented in both C and C++.
Extensions:
Multiple struct argument packs
int something(T pos0, U pos1, {}, {});
Low level compiler generated abstraction around the function call frame
__args.
int something(T pos0, U pos1, {} q, {} r) {
assert(&pos0 == &__args.pos[0]);
assert(&pos1 == &__args.pos[1]);
__args.pos[2]; //<-compiler error
__args.q.somearg;
__args.r.somearg;
__args.returnaddr; //int*, the return address. Would this be useful to
have? It could be abused to violate the type system by using it before the
return value has been constructed. It could be hooked into a memory
allocator strategy to use a specific memory strategy for return values.
__args.return; //int&, reference to the return value.
}
Anonymous struct lambda. Is this useful?
Enable this syntax:
auto h = { int x; int y; };
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_664_7945367.1402856244939
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">A possible solution to implementing named function paramet=
ers.<div><br></div><div>//declaration</div><div>//throws on error</div><div=
>void display(Screen& target, { int xres, int yres/*, etc..*/ }); //<=
;- compiler generated anonymous struct type</div><div><br></div><div>//defi=
nition</div><div>void display(Screen& target, {} p) { //<- p derives=
anonymous struct type from declaration. Error if no declaration present.<b=
r>doDisplayWork(target, p.xres, p.yres);</div><div>}</div><div><br></div><d=
iv>//At call site</div><div>Screen screen;</div><div>display(screen, {}); /=
/<-defaults</div><div>display(screen); //<-has a default set to {}, w=
orks like positional default args</div><div>display(screen, 640, 480); =
;</div><div>display(screen, yres=3D480, xres=3D640);</div><div>display(scre=
en, { yres=3D480, xres=3D640});</div><div><br></div><div><br></div><div>Thi=
s might have better support in ABI. The type of the function is derived fro=
m the type of the anonymous struct. This could be exploited to enhance erro=
r checking in the compiler.</div><div><br></div><div>A tool in clang could =
probably be written to convert legacy interfaces on large C++ code bases to=
decide how to use the new style on a case by case basis.</div><div><br></d=
iv><div>This idea could be implemented in both C and C++.</div><div><br></d=
iv><div><br></div><div>Extensions:</div><div><div>Multiple struct argument =
packs</div><div>int something(T pos0, U pos1, {}, {});</div></div><div><br>=
</div><div>Low level compiler generated abstraction around the function cal=
l frame __args.</div><div>int something(T pos0, U pos1, {} q, {} r) {<br>&n=
bsp; assert(&pos0 =3D=3D &__args.pos[0]);</div><div> assert(&=
amp;pos1 =3D=3D &__args.pos[1]);</div><div> __args.pos[2]; //<=
-compiler error</div><div> __args.q.somearg;</div><div> __args.=
r.somearg;</div><div> __args.returnaddr; //int*, the return address. =
Would this be useful to have? It could be abused to violate the type system=
by using it before the return value has been constructed. It could be hook=
ed into a memory allocator strategy to use a specific memory strategy for r=
eturn values. </div><div> __args.return; //int&, reference t=
o the return value.</div><div>}</div><div><br></div><div>Anonymous struct l=
ambda. Is this useful?</div><div>Enable this syntax:</div><div>auto h =3D {=
int x; int y; };</div><div><br></div><div> </div><div><br></div></div=
>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_664_7945367.1402856244939--
.