最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Can I make a consteval function throw a deprecation warning for given arguments? - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论