`#define`是c ++源代码中的一个非常大的数字

时间:2021-06-09 21:16:28

Well, the question is not as silly as it sound.

嗯,问题并不像听起来那么愚蠢。

I am using C++11 <array> and want to declare an array like this:

我正在使用C ++ 11 并想要声明一个这样的数组:

array<int, MAX_ARR_SIZE> myArr;

The MAX_ARR_SIZE is to be defined in a header file and could be very large i.e. 10^13. Currently I am typing it like a pre-school kid

MAX_ARR_SIZE将在头文件中定义,并且可能非常大,即10 ^ 13。目前我正在打字,就像一个学龄前的孩子

 #define MAX_ARR_SIZE 1000000000000000

I can live with it if there is no alternative. I can't use pow(10, 13) here since it can not be evaluated at compile time; array initialization will fail. I am not aware of any shorthand to type this.

如果别无选择,我可以忍受它。我不能在这里使用pow(10,13),因为它无法在编译时进行评估;数组初始化将失败。我不知道有任何类型的简写。

6 个解决方案

#1


2  

You can define a constexpr function:

您可以定义constexpr函数:

constexpr size_t MAX_ARR_SIZE()
{
    return pow(10, 15); 
}

That way you can do even more complex calculations in compile time.

这样,您可以在编译时进行更复杂的计算。

Then use it as array<int, MAX_ARR_SIZE()> myArr; it will be evaluated in compile time.

然后将它用作数组 myArr;它将在编译时进行评估。 ,max_arr_size()>

Also like it was already mentioned, you probably won't be able to allocate that size on the stack.

也就像已经提到的那样,你可能无法在堆栈上分配该大小。

EDIT:

I have a fault here, since pow itself is not constexpr you can't use it, but it's solvable, for example use ipow as discussed here: c++11 fast constexpr integer powers

我在这里有一个错误,因为pow本身不是constexpr你不能使用它,但它是可以解决的,例如使用ipow,如下所述:c ++ 11 fast constexpr integer powers

here is the function quote:

这是函数引用:

constexpr int64_t ipow(int64_t base, int exp, int64_t result = 1) {
  return exp < 1 ? result : ipow(base*base, exp/2, (exp % 2) ? result*base : result);
}

simply change MAX_ARR_SIZE() to:

只需将MAX_ARR_SIZE()更改为:

constexpr size_t MAX_ARR_SIZE()
{
    return ipow(10, 15); 
}

#2


9  

Using #define for constants is more a way of C than C++.

对于常量使用#define更像C语言而不是C ++。

You can define your constant in this way:

您可以通过以下方式定义常量:

const size_t MAX_ARR_SIZE(1e15); 

#3


6  

In this case, using a const size_t instead of #define is preferred.

在这种情况下,首选使用const size_t而不是#define。


I'd like to add that, since C++14, when writing integer literals, you could add the optional single quotes as separator.

我想补充一点,因为C ++ 14,在编写整数文字时,你可以添加可选的单引号作为分隔符。

1'000'000'000'000'000

This looks more clear.

这看起来更清楚。

#4


1  

#define MAX_ARRAY_SIZE (1000ull * 1000 * 1000 * 1000 * 1000)

#5


0  

You actually can evaluate pow(10, 15) and similar expressions at compile time in C++11 if you use a const instead of #define. Just make sure you pick a large enough primitive.

如果你使用const而不是#define,你实际上可以在编译时在C ++ 11中评估pow(10,15)和类似的表达式。只要确保你选择一个足够大的原语。

#6


0  

You can use :

您可以使用 :

#define MAX_ARR_SIZE 1e15

#define MAX_ARR_SIZE 1e15

1e15 is very huge and probably would not be allocated.

1e15非常庞大,可能不会被分配。

#1


2  

You can define a constexpr function:

您可以定义constexpr函数:

constexpr size_t MAX_ARR_SIZE()
{
    return pow(10, 15); 
}

That way you can do even more complex calculations in compile time.

这样,您可以在编译时进行更复杂的计算。

Then use it as array<int, MAX_ARR_SIZE()> myArr; it will be evaluated in compile time.

然后将它用作数组 myArr;它将在编译时进行评估。 ,max_arr_size()>

Also like it was already mentioned, you probably won't be able to allocate that size on the stack.

也就像已经提到的那样,你可能无法在堆栈上分配该大小。

EDIT:

I have a fault here, since pow itself is not constexpr you can't use it, but it's solvable, for example use ipow as discussed here: c++11 fast constexpr integer powers

我在这里有一个错误,因为pow本身不是constexpr你不能使用它,但它是可以解决的,例如使用ipow,如下所述:c ++ 11 fast constexpr integer powers

here is the function quote:

这是函数引用:

constexpr int64_t ipow(int64_t base, int exp, int64_t result = 1) {
  return exp < 1 ? result : ipow(base*base, exp/2, (exp % 2) ? result*base : result);
}

simply change MAX_ARR_SIZE() to:

只需将MAX_ARR_SIZE()更改为:

constexpr size_t MAX_ARR_SIZE()
{
    return ipow(10, 15); 
}

#2


9  

Using #define for constants is more a way of C than C++.

对于常量使用#define更像C语言而不是C ++。

You can define your constant in this way:

您可以通过以下方式定义常量:

const size_t MAX_ARR_SIZE(1e15); 

#3


6  

In this case, using a const size_t instead of #define is preferred.

在这种情况下,首选使用const size_t而不是#define。


I'd like to add that, since C++14, when writing integer literals, you could add the optional single quotes as separator.

我想补充一点,因为C ++ 14,在编写整数文字时,你可以添加可选的单引号作为分隔符。

1'000'000'000'000'000

This looks more clear.

这看起来更清楚。

#4


1  

#define MAX_ARRAY_SIZE (1000ull * 1000 * 1000 * 1000 * 1000)

#5


0  

You actually can evaluate pow(10, 15) and similar expressions at compile time in C++11 if you use a const instead of #define. Just make sure you pick a large enough primitive.

如果你使用const而不是#define,你实际上可以在编译时在C ++ 11中评估pow(10,15)和类似的表达式。只要确保你选择一个足够大的原语。

#6


0  

You can use :

您可以使用 :

#define MAX_ARR_SIZE 1e15

#define MAX_ARR_SIZE 1e15

1e15 is very huge and probably would not be allocated.

1e15非常庞大,可能不会被分配。