C++11: Compile Time Calculation of Array

前端 未结 7 2106
长发绾君心
长发绾君心 2020-11-28 05:25

Suppose I have some constexpr function f:

constexpr int f(int x) { ... }

And I have some const int N known at compile time:

Either<

7条回答
  •  心在旅途
    2020-11-28 06:16

    Here's a more concise answer where you explicitly declare the elements in the original sequence.

    #include 
    
    constexpr int f(int i) { return 2 * i; }
    
    template 
    struct sequence
    {
        using result = sequence;
        static std::array apply() { return {{Ts...}}; }
    };
    
    using v1 = sequence<1, 2, 3, 4>;
    using v2 = typename v1::result;
    
    int main()
    {
        auto x = v2::apply();
        return 0;
    }
    

提交回复
热议问题