Enforcing function contract at compile time when possible

后端 未结 3 1225
一生所求
一生所求 2021-02-02 00:12

(this question was inspired by How can I generate a compilation error to prevent certain VALUE (not type) to go into the function?)

Let\'s say, we have a single-argument

3条回答
  •  温柔的废话
    2021-02-02 01:13

    I got error with constexpr when used in constant expression for:

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

    Demo

    We cannot know that argument value is known at compile type, but we can use type representing value with std::integral_constant

    // alias to shorten name. 
    template 
    using int_c = std::integral_constant;
    

    Possibly with UDL with operator "" _c to have 5_c, 42_c.

    and then, add overload with that:

    template 
    constexpr auto foo(int_c) {
        return int_c{};
    }
    

    So:

    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
    

    As I said, arguments are not known to be constant inside the function, and is_constexpr seems not possible in standard to allow dispatch, but some compiler provide built-in for that (__builtin_constant_p), so with MACRO, we can do the dispatch:

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

    Demo

    Note: Cannot use foo(int_c{}) directly, even in if constexpr, as there is still some syntax check.

提交回复
热议问题