【问题标题】:Can I convert a non-const function argument to const and set the size of array?我可以将非常量函数参数转换为 const 并设置数组的大小吗?
【发布时间】:2018-03-28 04:25:04
【问题描述】:

数组需要一个常量来初始化大小。因此,int iarr[10]

我认为我可以采用非常量参数并将其转换为 const,然后将其用于数组大小

int run(int const& size);
int run(int const& size)
{
    const int csize = size;

    constexpr int cesize = csize;

    std::array<int, cesize> arr;
}

不幸的是,这不起作用,我想使用const_cast作为

int run(int& size);
int run(int& size)
{
    const int val = const_cast<int&>(size);

    constexpr int cesize = val;

    std::array<int, cesize> arr;
}

这也行不通。我已经阅读了一些 SO 帖子,看看我是否能找到任何东西

cannot-convert-argument-from-int-to-const-int
c-function-pass-non-const-argument-to-const-reference-parameter
what-does-a-const-cast-do-differently

当用作数组大小的初始值设定项时,有没有办法确保参数为 const?

编辑:我不是在问为什么我不能用非常量初始化一个数组。我在问如何从非常量函数参数初始化数组。因此,initialize-array-size-from-another-array-value 不是我要问的问题。我已经知道我不能这样做,但是下面可能提供了一种方法和答案。

【问题讨论】:

  • @JimmyB 该问题的 OP 真的 需要切换已接受的答案,尽管它并不直接适用于 OP 的示例
  • 看起来您正在尝试创建一个可变长度数组(这是一种具有各种缺点的高级 C 功能,但实际上在 C++ 中并不支持)。由于大小来自一个参数(并且您的函数不是constexpr),它的值只会在运行时知道。创建固定大小的数组需要在编译时知道大小,以便实例化正确的模板(或者通常分配正确的内存量)。
  • 思想实验:如果允许您刚刚展示的内容,为什么不直接放弃大小必须是常量的整个要求,因为解决方法只是将值分配给常量?
  • @Rakete1111 如果我被允许传递一个非常量参数来初始化一个数组或std::array 这个问题本身甚至都不存在。

标签: c++11 constants


【解决方案1】:

std::array 是一个不可调整大小的容器,其大小在编译时是已知的。

如果您在编译时知道您的大小值,您可以将值作为non-type template argument 传递:

template <int Size>
int run()
{
    std::array<int, Size> arr;
}

可以这样使用:

run<5>();

注意Size 必须是constant expression


如果您在编译时不知道自己的大小,请使用 std::vector 而不是 std::array

int run(int size)
{
    std::vector<int> arr;
    arr.resize(size); // or `reserve`, depending on your needs
}

std::vector 是一个可以在运行时调整大小的连续容器。

【讨论】:

  • @Mushy:这是基本的 C++ 知识。你读过 C++ 入门书吗?此外,std::arraystd::vector 的 cppreference 页面应该对您有所帮助。
  • 这可能是基本的 C++ 知识,但对你来说,如果我在一本书中读过这个,我就不会在 Stack 上问这个问题了。另外,我不想使用 std::vectorstd::array 需要 const 大小。没关系。
  • @Mushy:添加了更多信息和链接。它们应该包含您需要的所有信息。顺便说一句,我很难相信你的书在std::vector 和模板之前解释了constexprconst_cast。你读了什么书?
  • 我正在阅读 The C++ Programming Language (C++11) StroustrupC++ Primer (C++11) Lippman,而且,我在这两本书中的位置,Stroustrup 涵盖了 function templates、constexpr, and const. Lippman covered constexpr` 和 const,但两者都没有这本书表明我可以对数组执行此操作,到目前为止,这两本书都涵盖了数组。因此,我需要询问全世界的读者,因为这两本书都无法教授所有知识。
  • @Mushy:很公平 - 我的回答 + 链接应该对您有所帮助。如果您还有任何特别的问题,请告诉我。
【解决方案2】:

我在问如何从非常量函数参数初始化数组。

如您所见,不可能用变量初始化数组大小,因为您需要在编译器时指定大小或数组。

要解决您的问题,您应该使用std::vector,它像数组一样工作,但您可以在运行时调整它的大小。您可以像处理数组一样处理向量,使用运算符[],例如:

class MyClass
{
  vector<char> myVector; 
public:
  MyClass();

  void resizeMyArray(int newSize);
  char getCharAt(int index);
};

MyClass::MyClass():
  myVector(0) //initialize the vector to elements
{

}

void MyClass::resizeMyArray(int newSize)
{
  myVector.clear();
  myVector.resize(newSize, 0x00);

}

char MyClass::getCharAt(int index)
{
  return myVector[index];
}

欲了解更多信息,请查看此链接:http://www.cplusplus.com/reference/vector/vector/

升级:另外,考虑到std::array 不能调整大小,因为links 说:

数组是固定大小的序列容器:它们包含按严格线性序列排列的特定数量的元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2021-12-08
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多