vendredi 28 octobre 2016

c++ bool template avoiding if statement

I'm looking a way to avoid if-statement when I'm dealing with template bool function.
Code below shows a simplification of my situation.

#include <iostream>
#include <string>

template<bool var>
void f(){
 std::cout << (var ? "TRUE" : "FALSE") << std::endl;
}

int main(int argc, char* argv[]){
    const bool b = (std::string(argv[1]).compare("TRUE") == 0);
    if (b) f<true>();
    else f<false>();
    return 0;   
}

I would like a way to write something like: f<b>();
But doing so, I obtain the following error:
error: the value of ‘b’ is not usable in a constant expression

Since in my application I have 4 bool templates, I would like to avoid to list all the combination of those four, like:

if(b1 && b2 && b3 && b4) f<true,true,true,true>();
else if (b1 && b2 && b3 && !b4) f<true,true,true,false>();
...

There is a way around? Does exists a shortcut that I can use in some way?
I tried also to use the if-statement shortcut f<(b?true:false)>();,
but I received the same error shows above.

Aucun commentaire:

Enregistrer un commentaire