简体   繁体   English

通过作为 const 传递给 function 来设置结构中数组的大小 - 非类型模板参数不是常量表达式

[英]Setting the size of array in a struct by passing as a const to a function - Non-type template argument is not a constant expression

User sets k at running time.用户在运行时设置k This number will be constant for the rest of the code.对于代码的 rest,此数字将保持constant I want to create a function to pass and create a struct that includes an array of size k with that number.我想创建一个 function 来传递并创建一个结构,该结构包含一个大小为 k 的数组,该数组具有该数字。 However, the compiler returns this error:但是,编译器返回此错误:

Non-type template argument is not a constant expression

Any recommendation will be appreciated.任何建议将不胜感激。

The code is like:代码如下:

template <int N>
struct UL {
    unsigned long ul [N];
};

void func(const int k){
    UL<k> x;  //problem is here
}

int main () {
    int k;
    cin >> k;
    func(k);
    return 0;
}

Variable k is set at runtime, so when the code is being compiled, the compiler doesn't know what the value of k is.变量k是在运行时设置的,所以在编译代码时,编译器不知道k的值是什么。 You can't do this.你不能这样做。 But if you know what your k value is going to be and the range of values it can be is limited, you can create your struct for each possible value of k and choose the matching class at runtime.但是,如果您知道您的k值将是什么并且它的值范围是有限的,您可以为每个可能的k值创建您的结构,并在运行时选择匹配的 class。 Of course this is not probably what you want.当然,这可能不是您想要的。 You just need to be able to distinguish between what is known at compile-time and at runtime.您只需要能够区分编译时和运行时已知的内容。 Templated literals (I hope I used the right name) are a compile-time feature of C++.模板文字(我希望我使用了正确的名称)是 C++ 的编译时特性。

Templates are processed at compile-time only.模板仅在编译时处理。 You can't pass a run-time variable, like a function parameter, to a template.您不能将运行时变量(例如 function 参数)传递给模板。 For what you are trying to do, you will have to use std::vector instead, eg:对于您要执行的操作,您必须改用std::vector ,例如:

#include <vector>

struct UL {
    std::vector<unsigned long> ul;
};

void func(const int k){
    UL x;
    x.ul.resize(k);
}

int main () {
    int k;
    cin >> k;
    func(k);
    return 0;
}

A fundamental principle about templates is that:关于模板的一个基本原则是:

Any template argument must be a quantity or value that can be determined at compile time .任何模板参数都必须是可以在编译时确定的数量或值。

This has dramatic advantages on the runtime cost of template entities.这对模板实体的运行时成本具有显着优势。

But in your example, k is not a compile time constant and you're using it as a template argument and so as a consequence of the above quoted statement you get the error.但是在您的示例中, k不是编译时间常数,并且您将其用作模板参数,因此由于上述引用的语句,您会收到错误。

To solve your problem you can use a std::vector as shown below:解决您的问题,您可以使用std::vector ,如下所示:

#include <iostream>
#include <vector>

struct UL {
    std::vector<unsigned long> ul;
    
    //constructor
    UL(int k): ul(k) //this creates vector ul of size k
    {
        std::cout<<"size of vector set to: "<<ul.size()<<std::endl;
    }
};

void func(const int k){
    UL x(k);  //pass k as argument to constructor
}

int main () {
    int k; 
    std::cin >> k;
    
    func(k);
    return 0;
}

The output of the program can be seen here .程序的output可以看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM