首页 > 解决方案 > 尽可能在编译时强制执行函数契约

问题描述

(这个问题的灵感来自How can I generate a compiler error to prevent certain VALUE (not type) to go into the function?

比方说,我们有一个单参数foo,语义上定义为

int foo(int arg) {
    int* parg;
    if (arg != 5) {
        parg = &arg;
    }

    return *parg;
}

上面的整个代码用于说明一个简单的想法 - 函数返回它自己的参数,除非参数等于 5,在这种情况下,行为是未定义的。

现在,挑战 - 以这样的方式修改函数,如果它的参数在编译时已知,则应该生成编译器诊断(警告或错误),如果不是,则行为在运行时保持未定义。解决方案可能依赖于编译器,只要它在四大编译器之一中可用。

以下是一些无法解决问题的潜在路线:

标签: c++

解决方案


constexpr常量表达式中使用时出现错误:

constexpr int foo(int arg) {
    int* parg = nullptr;
    if (arg != 5) {
        parg = &arg;
    }
    return *parg;
}

演示

我们无法知道参数值在编译类型中是已知的,但我们可以使用类型表示值std::integral_constant

// alias to shorten name. 
template <int N>
using int_c = std::integral_constant<int, N>;

可能使用UDLoperator "" _cto have 5_c, 42_c.

然后,添加重载:

template <int N>
constexpr auto foo(int_c<N>) {
    return int_c<foo(N)>{};
}

所以:

foo(int_c<42>{}); // OK
foo(int_c<5>{}); // Fail to compile

// and with previous constexpr:
foo(5); // Runtime error, No compile time diagnostic
constexpr auto r = foo(5); // Fail to compile

正如我所说,参数在函数内部不知道是恒定的,并且is_constexpr在标准中似乎不可能允许分派,但是一些编译器为此提供了内置的 ( __builtin_constant_p),所以使用 MACRO,我们可以进行分派:

#define FOO(X) [&](){ \
    if constexpr (__builtin_constant_p(X)) {\
        return foo(int_c<__builtin_constant_p (X) ? X : 0>{});\
    } else {\
        return foo(X); \
    } \
}()

演示

注意:不能foo(int_c<X>{})直接使用,即使在 if constexpr 中,因为仍然有一些语法检查。


推荐阅读