Thread overview | |||||
---|---|---|---|---|---|
|
April 10, 2011 Re: "optional" func alias template param | ||||
---|---|---|---|---|
| ||||
On 04/10/2011 04:10 PM, spir wrote: > Hello, > > I need a trick to allow a function template parameter be optional. > The following (reduced case) fails because D wants to call f: > > uint f(uint i) { return i; } > struct S (alias func=null) { > enum bool hasFunc = !(func == null); // *** error line *** > } > unittest { > // ok > auto s1 = S!()(); > writeln(s1.hasFunc); > // not ok > auto s2 = S!(f)(); > writeln(s2.hasFunc); > } > ==> > Error: function ___.f (uint i) is not callable using argument types () > (I consider that a bug due to "f" implicitely meaning "f()" --but this is > another story.) > > The following also fails: > > // not ok > auto s3 = S1!(&f)(); > writeln(s3.hasFunc); > ==> > Error: expression & f is not a valid template value argument PS: I also cannot pass a typed func: struct S (uint delegate (uint) func=null) { enum bool hasFunc = !(func == null); } ==> Error: arithmetic/string type expected for value-parameter, not uint delegate(uint) -- _________________ vita es estrany spir.wikidot.com |
April 10, 2011 Re: "optional" func alias template param | ||||
---|---|---|---|---|
| ||||
On 04/10/2011 04:10 PM, spir wrote: > Hello, > > I need a trick to allow a function template parameter be optional. > The following (reduced case) fails because D wants to call f: > > uint f(uint i) { return i; } > struct S (alias func=null) { > enum bool hasFunc = !(func == null); // *** error line *** > } > unittest { > // ok > auto s1 = S!()(); > writeln(s1.hasFunc); > // not ok > auto s2 = S!(f)(); > writeln(s2.hasFunc); > } > ==> > Error: function ___.f (uint i) is not callable using argument types () > (I consider that a bug due to "f" implicitely meaning "f()" --but this is > another story.) Found one solution using isCallable! Other tricks welcome... uint f(uint i) { return i; } struct S (alias func=null) { enum bool hasFunc = isCallable!(typeof(func)); } unittest { auto s1 = S!()(); writeln(s1.hasFunc); auto s2 = S!(f)(); writeln(s2.hasFunc); } I'd also like to know why pointer cannot be template *alias* parameters, like in: auto s2 = S!(&f)(); ==> Error: expression & f is not a valid template value argument Denis -- _________________ vita es estrany spir.wikidot.com |
April 10, 2011 Re: "optional" func alias template param | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | On 10/04/2011 15:31, spir wrote: > I'd also like to know why pointer cannot be template *alias* parameters, > like in: > auto s2 = S!(&f)(); > ==> > Error: expression & f is not a valid template value argument > > Denis First of all, that error is useless, you should probably report a bug for that (particularly if it doesn't include a line number, I'm not sure if you've omitted that or not). Secondly, alias parameters are used to pass a symbol to a template, &f is not a symbol, it's a pointer to the value held in symbol f. -- Robert http://octarineparrot.com/ |
Copyright © 1999-2021 by the D Language Foundation