Is there any performance penalty for static if?

user1234 user1234 at 12.de
Thu May 16 00:18:25 UTC 2019


On Wednesday, 15 May 2019 at 22:03:39 UTC, Ferhat Kurtulmuş wrote:
> Hi,
>
> Maybe I already know the answer, but have to be sure about 
> this. I am emulating this cpp code "int val = mat.at<int>(row, 
> col)" like:
>
> T at(T)(int row, int col){
>         static if (T.stringof == "float"){
>             return getFloatAt(row, col);
>         } else static if (T.stringof == "double"){
>             return getDoubleAt(row, col);
>         } else static if (T.stringof == "int"){
>             return getIntAt(row, col);
>         } else static if (T.stringof == "ubyte"){
>             return getUCharAt(row, col);
>         } else static if (T.stringof == "byte"){
>             return getSCharAt(row, col);
>         } else static if (T.stringof == "short"){
>             return getShortAt(row, col);
>         }
>     }
> This works as expected, and I know conditions of "static if" is 
> determined at compile time  and I assume no speed penalty here?

You've been given the answer but about this particular piece of 
code, rather use the "is" expression

   static if (is(T == float)) {}
   else static if (is(T == double)) {}

etc.


More information about the Digitalmars-d-learn mailing list