Referencing an overloaded function
Timon Gehr
timon.gehr at gmx.ch
Sat Mar 24 18:44:13 PDT 2012
On 03/25/2012 12:34 AM, Artur Skawina wrote:
> On 03/24/12 23:10, Timon Gehr wrote:
>> On 03/24/2012 09:07 PM, Philippe Sigaud wrote:
>>> On Sat, Mar 24, 2012 at 18:07, Artur Skawina<art.08.09 at gmail.com> wrote:
>>>
>>>>> foreach (f; __traits(getOverloads, __traits(parent, main), "foo")) {
>>>
>>> Hey, this
>>> ^^^^^^^^^^^^^^^^^^^^^^
>>> it's a way to get the current module, right? Nice trick, I didn't think of this.
>>>
>>> Its limitation is it won't work outside the 'main() {}' module.
>>
>>
>> You can get the parent of an alias. The following template finds all overloads of a given symbol:
>>
>> template ID(T...){alias T ID;}
>> template getOverloads(alias f){
>> enum fname = f.stringof[0..f.stringof.indexOf("(")];
>> alias ID!(__traits(getOverloads, __traits(parent, f), fname)) getOverloads;
>> }
>>
>> I think this should go into std.traits, as soon as it works. (DMD is buggy when it comes to taking the .stringof of a function. It misinterprets it as a property function call in some cases but not in others. Messy.)
>
> Yeah, your version does not work with my old GDC either (stringof returns really weird
> things here...); this one works:
>
> template ID(T...) { alias T ID; }
> template getOverloads(alias F) {
> alias ID!(__traits(getOverloads, __traits(parent, F),
> __traits(identifier, F)))
Nice one.
> getOverloads;
> }
>
> and looks obvious enough; it would be nice to get rid of that extra ID step,
> though.
>
> artur
More information about the Digitalmars-d-learn
mailing list