I have a template
template<size_t N>
class Foo {
int bar(int a) {
if (N == 0)
return 0;
return a / N;
}
}
when I instantiate this with 0
Foo<0> bar;
gcc is too smart and reports division by zero at compile time
I tried
class Foo<size_t N> {
template<size_t M>
int bar(int a) {
return a / N;
}
template<>
int bar<0>(int a) {
return 0;
}
};
but this gives me error:
error: explicit specialization in non-namespace scope 'class Foo' error: template-id 'bar<0>' in declaration of primary template
any ideas how I could solve/workaround this?
You can always rethink the formula:
template<size_t N>
class Foo {
bool bar() {
return N == 0 || (N >=5 && N < 10);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With