"optional" func alias template param
spir
denis.spir at gmail.com
Sun Apr 10 07:31:12 PDT 2011
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
More information about the Digitalmars-d-learn
mailing list