www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: quirks of functions and delegates

reply Ender KaShae <astrothayne gmail.com> writes:
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 } :)

This doesn't quite work, when only a function type is passed I get errors saying that the template was not instatiated correctly and a ton of weird errors about function pointer not implicitly casting to int's, I have no idea what that's about however when the return and paramater types are explicitly given it runs fine, though it's a little inconvienent to have to supply those twice
 
 2.) 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.

on that note, if you have a function int func(); and evaluate typeid(typeof(func)) you get the type int() which is odd since func is evaluated the same as func() so the type should just be int
Aug 01 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ender KaShae" <astrothayne gmail.com> wrote in message 
news:f8qffl$er3$1 digitalmars.com...
 This doesn't quite work, when only a function type is passed I get errors 
 saying that the template was not instatiated correctly and a ton of weird 
 errors about function pointer not implicitly casting to int's, I have no 
 idea what that's about
 however when the return and paramater types are explicitly given it runs 
 fine, though it's a little inconvienent to have to supply those twice

I'm going to have to see some of your code. This works fine for me: template Templ(T : U function(V), U, V...) { const Templ = T.stringof; } void main() { pragma(msg, Templ!(int function(float))); pragma(msg, Templ!(void function(char[], int[char[]]))); }
 so what exactly does Foo mean (without the & operator) and why would 
 anyone want a Foo[] (whatever that means)  it seems to me that Foo is a 
 symbol, not a type.

Foo is a type. You can make pointers by adding an asterisk to it like any other type: Foo*. It's not a very useful type, it's just there for completeness.
 on that note, if you have a function int func(); and evaluate 
 typeid(typeof(func)) you get the type int() which is odd since func is 
 evaluated the same as func() so the type should just be int

Again, I'm going to have to see code. function int func() is an incomplete function literal. It means nothing and does not compile. However if you write "int function() func;" and then get the typeid(typeof(func)), it's "int()*" (that is a pointer to a function which takes nothing and returns an int), which makes sense because the "call a function without parens" does not apply to function _pointers_, only _functions_ (there's a use for that function type!).
Aug 01 2007