www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - having a trivial anonymous function call in template prevents

reply Cauterite <cauterite gmail.com> writes:
// ------ Example: ----------

template A(alias Arg) {
	enum A = Arg;
	enum Unrelated = ({return 0;})(); // this line prevent 
compilation
};

void main() {
	enum FnPtr = &asdf;
	enum _ = A!FnPtr;
};

void asdf() {};

// ( https://dpaste.dzfl.pl/79301f12e5fc )

Just by having a random `({return 0;})()` in the template body, 
suddenly the template rejects its arguments. I'm so confused, is 
this a bug? Or am I missing something?
Aug 17 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 17 August 2016 at 13:09:40 UTC, Cauterite wrote:
 Just by having a random `({return 0;})()` in the template body, 
 suddenly the template rejects its arguments. I'm so confused, 
 is this a bug? Or am I missing something?
Function pointers and delegates are not valid compile time variables. Best you can do is use them in an alias argument directly, but you cannot use them in an enum argument.
Aug 17 2016
parent reply Cauterite <cauterite gmail.com> writes:
On Wednesday, 17 August 2016 at 13:18:06 UTC, Adam D. Ruppe wrote:
 Best you can do is use them in an alias argument directly, but 
 you cannot use them in an enum argument.
I think you missed the point; it works perfectly fine without having this `({return 0;})()` in the template body (which, as far as I can see, doesn't appear to interact at all with the template argument).
Aug 17 2016
parent reply Lodovico Giaretta <lodovico giaretart.net> writes:
On Wednesday, 17 August 2016 at 13:21:16 UTC, Cauterite wrote:
 On Wednesday, 17 August 2016 at 13:18:06 UTC, Adam D. Ruppe 
 wrote:
 Best you can do is use them in an alias argument directly, but 
 you cannot use them in an enum argument.
I think you missed the point; it works perfectly fine without having this `({return 0;})()` in the template body (which, as far as I can see, doesn't appear to interact at all with the template argument).
I think he meant that ({ return 0;})() cannot be executed at compile time and assigned to an enum. That's why the instantiation is failing.
Aug 17 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/17/16 9:23 AM, Lodovico Giaretta wrote:
 On Wednesday, 17 August 2016 at 13:21:16 UTC, Cauterite wrote:
 On Wednesday, 17 August 2016 at 13:18:06 UTC, Adam D. Ruppe wrote:
 Best you can do is use them in an alias argument directly, but you
 cannot use them in an enum argument.
I think you missed the point; it works perfectly fine without having this `({return 0;})()` in the template body (which, as far as I can see, doesn't appear to interact at all with the template argument).
I think he meant that ({ return 0;})() cannot be executed at compile time and assigned to an enum. That's why the instantiation is failing.
Yes, it can: https://dpaste.dzfl.pl/fca15065a4cf I think the OP's case is a bug. Please file. -Steve
Aug 17 2016
parent Cauterite <cauterite gmail.com> writes:
On Wednesday, 17 August 2016 at 13:33:26 UTC, Steven 
Schveighoffer wrote:
 I think the OP's case is a bug. Please file.
Thanks, I've filed it. Just wanted to get a second opinion before concluding that it's a bug.
Aug 17 2016