Multiple Member Constexpr Struc Initialization

霸气de小男生 提交于 2021-01-29 19:17:49

问题


I am new to constexpr, however, I believe that problem I am practicing is a good fit compile-time calculations. This is not for homework or anything, just practicing "Modern" C++.

Here is what I have so far: Header (prime_app_lib.hpp):

// #pragma once for modern header files (.hpp)
#pragma once

#include <math.h>

static constexpr unsigned max_prime = 10000U;
// check out this link for the number of primes: https://primes.utm.edu/howmany.html
static constexpr unsigned prime_array_size = 1230U;

constexpr unsigned get_nth_prime(unsigned n);

I am trying to create a const array (constructed at compile time) that I can access with get_nth_prime(n). You can see the algorithm I am trying to implement with the structure that I chose. No matter what I tried, I can't initialize both isPrime and primeValue. I am open to any suggestions, especially a full rewrite. I would just like something that works following good and modern C++.

#include "prime_app_lib.hpp"
#include <iostream>

struct PrimeData {
    bool isPrime[max_prime];
    unsigned primeValue[prime_array_size];

    // Constructor for the data using SieveOfEratosthenes
    // https://www.geeksforgeeks.org/program-to-find-the-nth-prime-number/
    
    constexpr PrimeData() {
        for (auto b : PrimeData::isPrime) {
            b = true;
        }
        for (auto p : PrimeData::primeValue) {
            p = 0;
        }

        for (int p = 2; p * p < max_prime; p++)
        {
            // If IsPrime[p] is not changed, then it is a prime  
            if (PrimeData::isPrime[p] == true)
            {
                // Update all multiples of p greater than or   
                // equal to the square of it  
                // numbers which are multiple of p and are  
                // less than p^2 are already been marked.   
                for (int i = p * p; i < max_prime; i += p)
                    PrimeData::isPrime[i] = false;
            }
        }

        // Store all prime numbers  
        int n = 0;
        for (int p = 2; p < max_prime; p++) {
            if (PrimeData::isPrime[p] && n < prime_array_size)
                PrimeData::primeValue[n] = static_cast<unsigned>(p);
        }
    }
};

// this was just to make sure I can get some constexpr compiling
constexpr unsigned get_nth_prime(unsigned n) {  
    return unsigned(prime_array_size);

}

int main() {
    std::cout << prime_array_size << "\n";
    constexpr unsigned test1 = get_nth_prime(2);
    static_assert(test1 == prime_array_size, "test1");
}

回答1:


The issue is all your members need to be initialized before entering the constructor body. So you could do:

constexpr PrimeData() : isPrime{} , primeValue{} {
  // ...
}

or simply:

struct PrimeData {
    bool isPrime[max_prime]{};
    unsigned primeValue[prime_array_size]{};
 
    // ...
};

Here's a demo.



来源:https://stackoverflow.com/questions/62903653/multiple-member-constexpr-struc-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!