About specialization with ints, and static_assert

Viewed 108

I want to write a template function that only work with 2 numbers (for instance 3 and 5) and gives and error if you try to use it with another numbers.

I can do this in this way:

template<int x>
void f();

template<>
void f<3>()
{
   cout << "f<3>()\n";
}


template<>
void f<5>()
{
  cout << "f<5>()\n";
}

and then I can call this function the normal way:

f<3>();
f<5>();

and it compiles well, and if I try to use my function incorrectly:

f<10>();

the compiler gives me an error.

I have 2 problems with this approach:

1.- Is this standard? Can I specialized a template with ints?

2.- I don't like the error you get if you use this approach, because the error doesn't tell the user what he had done incorrectly. I'd prefer write something like:

template<int x>
void f()
{
    static_assert(false, "You are trying to use f with the wrong numbers");
}

but this doesn't compile. It appears my compiler (gcc 5.4.0) is trying to instantiate first the primary template, and because of that it gives the error (of the static_assert).

Thank you for your help.

In case you are wondering why I want to do this is because I am learning how to program a microcontroller. In a microcontroller you have some pins that only do some things. For instance, the pins 3 and 5 are the pins in which you can generate a square wave. If in an application I want to generate a square wave I want to write somthing like:

square_wave<3>(frecuency);

But, if some months later I want to reuse this code (or change it) in another application with a differente microcontroller, I want my compiler say to me: "eh, in this microcontroller you can't generate a square wave in pins 3 and 5. Instead, use pins 7 and 9". And I think this can save me a lot of headaches (or maybe not, I really don't know. I'm just learning how to program a microcontroller).

3 Answers
Related