digitalmars.D - Re: quirks of functions and delegates
- Ender KaShae <astrothayne gmail.com> Jul 31 2007
- BCS <ao pathlink.com> Jul 31 2007
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Jul 31 2007
Jarrett Billingsley Wrote:"Ender KaShae" <astrothayne gmail.com> wrote in message news:f8ghug$5bg$1 digitalmars.com...1.) when I try template t(type: function) I get an error, but there must be some way to specify that you need the type to be a function
template Templ(T : U function(V), U, V...) { } void main() { mixin Templ!(int); // fails mixin Templ!(void function(int, float)); // OK } :)
I tried that, however when instantiating the template the return type and paramater tuple must be explicitly given, using a static if statement can be used to the same affect however2.) in an example in the docs it says that arrays of functions are invalid types in c++ and d, however i've used arrays of function pointers in c++ and it seems strange that such a type would be invalid, a function pointer is after all just a pointer
There's a slight difference. A function pointer is valid in both languages, but a function type is illegal. It's very difficult to get at a function type in D, but possible. Consider: typedef void Foo(); Foo[] f; typedef void function() Bar; Bar[] g; Notice that the first defines Foo as a function -- not function _pointer_ -- type. Foo[] f; fails. But the second defines Bar as a function pointer, and Bar[] g is fine.
Jul 31 2007
Reply to Ender,Jarrett Billingsley Wrote:"Ender KaShae" <astrothayne gmail.com> wrote in message news:f8ghug$5bg$1 digitalmars.com...1.) when I try template t(type: function) I get an error, but there must be some way to specify that you need the type to be a function
{ } void main() { mixin Templ!(int); // fails mixin Templ!(void function(int, float)); // OK } :)
and paramater tuple must be explicitly given, using a static if statement can be used to the same affect however
might this work? (I haven't tried it yet) template T(F) { staic if(is(F R == function)) else static assert(false); }
Jul 31 2007
"BCS" <ao pathlink.com> wrote in message news:ce0a3343c58c8c9a19db144d622 news.digitalmars.com...might this work? (I haven't tried it yet) template T(F) { staic if(is(F R == function)) else static assert(false); }
shorter: template T(F) { static assert(is(F == function)); }
Jul 31 2007