I have a function like:
consteval int func(int n) {
...
}
I would like to throw a deprecation warning if passed certain argument values, e.g. if the argument is less than 5:
constexpr int a = func(6); // ok
constexpr int b = func(4); // warning: deprecated
I cannot change the signature of func
- for example, to something like func<N>()
or func(EnumType)
. This is because it's part of a legacy API for which I'm trying to add additional static analysis benefits.
It is important that it is particular values of the functions which are deprecated, not the use of any symbols that might be used to generate that value. This can be achieved like this:
template <int N> requires (N < 5)
[[deprecated]] consteval int funcT() {
return 0;
}
template <int N> requires (N >= 5)
consteval int funcT() {
return N;
}
constexpr int a = funcT<4>(); // deprecated
constexpr int b = funcT<6>(); // ok
... but this cannot be done because of the legacy API constraint.
Is this possible? If I have a body like:
[[deprecated]] constexpr void deprecatedFunctionToTriggerWarning() {}
consteval int func(int n) {
if (n < 5) {
deprecatedFunctionToTriggerWarning();
return 0;
}
return n;
}
... then I get a deprecation warning regardless even if the branch is not taken.