I am trying to write a function such that if called with compile-time-constant arguments it will trigger a compile-time error if the value of the argument doesn't match a static_assert
, but can still be called at run-time with computed values.
Something kinda like this:
template<int N> void f(N){
static_assert(N == 5, "N can only be 5.");
do_something_with(N);
}
void f(int N){
if(N == 5){
do_something_with(N);
}
}
volatile int five = 5;
volatile int six = 6;
int main() {
f(5); //ok
f(6); //compile-time error
f(five); //ok
f(six); //run-time abort
return 0;
}
How can I do this?
Also, if possible I would like to be able to keep the simple f(something)
syntax, because this code is intended for a library that should be usable by beginner programmers that aren't familiar with template syntax.
Aucun commentaire:
Enregistrer un commentaire