多成员Construpr结构初始化 [英] Multiple Member Constexpr Struc Initialization

查看:53
本文介绍了多成员Construpr结构初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是constexpr的新手,但是,我认为我正在练习的问题很适合编译时计算.这不是为了做家庭作业或任何事情,而只是练习现代"练习.C ++.

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++.

这是我到目前为止所拥有的:标题(prime_app_lib.hpp):

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);

我正在尝试创建一个const数组(在编译时构造),我可以使用get_nth_prime(n)访问它.您可以看到我尝试使用所选结构实现的算法.无论我尝试了什么,都无法同时初始化isPrime和primeValue.我愿意接受任何建议,尤其是全面重写.我只希望能遵循良好和现代的C ++进行工作.

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");
}

推荐答案

问题是进入构造函数主体之前,需要初始化所有成员.因此,您可以这样做:

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

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

或简单地:

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

这是一个演示.

这篇关于多成员Construpr结构初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆