You can create a custom specialization for Foo<0>
.
template <> class Foo<0> { public: bool bar () { return true; } };
If you want to solve the problem only with bar
and not touch any other part of Foo
, you can create a companion method to avoid the problem:
template <size_t N> class Foo { bool bar(int n) { if (n == 0) return true; return 5 / n == 1; } public: bool bar() { return bar(N); } };
Or pull the implementation of this method into your class and indicate that:
template <size_t N> class Bar { public: bool operator() const { return 5 / N == 1; } }; template <> class Bar<0> { public: bool operator() const { return true; } }; template <size_t N> class Foo { bool bar() { return Bar<N>()(); } };
Alternatively, you can use the Jarod42 proposal and specialize the method itself (the answer is repeated here for completeness).
template <size_t N> class Foo { public: bool bar() { return 5 / N == 1; } }; template <> inline bool Foo<0>::bar() { return true; }
source share