Template variable not reach at compile

Mike Parker aldacron at gmail.com
Sat Jul 21 13:13:11 UTC 2018


On Saturday, 21 July 2018 at 12:17:54 UTC, Greatsam4sure wrote:
> Sorry for the typo.  This is the problem
>
> auto arithmetic(T, V, U)(T a,  V b,  U op){
>    return mixin("a"~op~"b");
> }
>
> //call like this
> arithmetic(1.5,2.5,"+");
>
>
> Compiler says the variable op is not reach at compile time.  So 
> how can the varible a and b be reach at compile time and op is 
> not reach. I will appreciate any help. I have also use a static 
> if but the same complain.  Just a newbie in D

You aren't using a or b at compile time -- you're using the 
string literals "a" and "b", which have nothing at all to do with 
a and b. In order to get what you want, you need to make op a 
template parameter like this:

auto arithmetic(string op, T, V)(T a, V b)
{
     mixin("a"~op~"b");
}

arithmetic!("+")(1.5, 2.5);



More information about the Digitalmars-d-learn mailing list