问题标签 [compile-time-constant]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
438 浏览

d - D中的固定大小位数组

如果我想要一个编译时大小的位数组(在堆栈上),我的选择是什么?

我在想类似的东西

以及bt来自core.bitop

会满足我的需要。

是否有人已经为此编写了模板以及一些不错的布尔运算?

0 投票
2 回答
642 浏览

c++ - Compile-time access to class template's member by index

Given a template whose non-type parameter determines the size of a non-const int array member, how can I access the array elements by an integral index at compile time? I want the access to be done via the class template’s getter method at.

I figured since class templates must be instantiated before runtime, I can pass another non-type class template’s enum member value to the prior class’s at method to ensure the index argument is a compile-time constant.

I left the class template deliberate_error undefined to see if its arguments are computed at compile time and to view the compile-time results in the error messages.

aw.at(cti); doesn’t give an error, so I thought that if I passed the same expression to deliberate_error instance my_error2, the compiler will display the value of arr[2] in the error message.

my_error1 causes g++ error: aggregate 'deliberate_error<2u> my_error1' has incomplete type and cannot be defined,

showing cti’s wrapped integral value 2. So, I thought if I passed the same cti to object aw's getter, and then pass the result to my_error2, I can get arr[2] in the error message. But instead, it prints:

So, I tried prepending constexpr to aw’s declaration, but that gives even more undesirable errors. What’s wrong here, and how can I fix it?

0 投票
0 回答
440 浏览

c++ - 如何实现编译时间获取器,以获得更简洁的调用?

我想以一种使其调用更简洁的方式实现编译时 getter。我有一个非类型 ( unsigned int N) 类模板foo,它递归地继承自foo<N - 1>. 每一个foo都有自己的enum成员number,它被初始化为其对应的值N。基本情况是foo<0>。例如,一个foo<5>对象有 6 个enum称为number,值0通过5。我想实现一个编译时 getter at,但这并不像constexpr在原型前面添加 a 那样简单:

在 g++ 4.8 中,我得到了几个错误实例:error: 'index' is not a constant expression除其他外。其余的只是跟风。即使所有客户端代码at仅使用整数文字调用,getter 也不会编译。为什么?

无论如何,我的解决方案是实现一个编译时整数包装器。它只是一个非类型 ( unsigned int N) 类模板ctint(compile-time int 的缩写),其enum成员mem被初始化为其N

因此,将foo<N>foo<0>的 getter 方法分别替换为:

作品:

但使函数调用变得冗长。我想要一个没有库的实现,除了<iostream>显示某些工作但无助于使其工作的部分(例如)。我希望 getter 能够<>在调用中选择性地省略或没有语法或拼写类型名称,但不一定在原型或定义中。我不希望它涉及array[]s。如果那是不可能的,我想知道下一个最好的方法。

0 投票
1 回答
705 浏览

c - 铿锵声中是否有“积分常数溢出”警告?

考虑以下片段:

我们可以在 Clang 中得到警告(/错误)吗?如何?从什么版本开始?

谢谢,西普里安。

0 投票
3 回答
1600 浏览

scala - Is there a way to test at compile-time that a constant is a compile-time constant?

Given how difficult it is to know whether an arithmetic final val expression will be compiled to a compile-time constant, and how easy it is to accidentally break compile-time-ness...

Can anyone think of an easy way to verify, at compile-time, that the compiler has actually created a compile-time constant from, say, a complex arithmetic expression? I'm guessing this might be some kind of annotation or macro, but maybe there's something simpler. For example, maybe something like:

would be possible.

0 投票
1 回答
1578 浏览

c++ - Does "int size = 10;" yield a constant expression?

The following code compiles under gcc 4.8 and Clang 3.2:

8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a bug in both compilers, or am I missing something?

The latest VC++ CTP rejects the code with this interesting message:

The interesting part is how it seems to think that size is zero. But at least it rejects the code. Shouldn't gcc and Clang do the same?

0 投票
2 回答
4751 浏览

c - XCode:初始化器元素不是编译时常量

我正在使用 XCode 用 C 语言编写程序。我很少使用C,通常我使用C++。我以前从未使用过 XCode。

编译错误很简单,以下代码行没有被编译器视为编译时常量。

我确信这在 C++ 11 中是允许的,尽管我不能确定,因为我几个月前最后一次使用它。

我的猜测是 XCode 编译器/C 标准不允许以这种方式计算常量。

有没有我可以使用的替代方案?我不太喜欢“定义”的替代方案......

因为这会(可能?)导致不必要的运行时开销。

0 投票
1 回答
72 浏览

c++ - 公开私有内部班级规模

我需要用内部类的大小公开一个编译时间常数。为此,我尝试了下一个代码:

我的问题是PrivateSize在某些编译器上签名而在其他编译器上未签名,并且在将其与签名类型进行比较时收到警告。据我所知enum,底层类型是实现定义的,不能强制为signedor unsigned

您是否知道一种将sizeof(A::Private)外部公开A为编译时间常数但保持Private类...私有的方法?请注意我不能使用constexpr,因为代码将在一些旧的编译器上使用。

0 投票
1 回答
272 浏览

c# - C# const int、const someStruct 的区别。为什么 const someStruct 不是“编译时常量”?

我想知道“编译时常量”到底是什么。从这里开始

这只是意味着标记为 const 的成员的每个实例都将在编译期间替换为其值,而 readonly 成员将在运行时解析。

所以我假设如果我的 colorBlack 为 const,那么它将是编译时常量,但编译器会告诉我不然。但它不会抱怨“const int some int = 0;” 是编译时常量。

为什么?

0 投票
1 回答
4445 浏览

gcc - 诸如 GCC 之类的 C/C++ 编译器通常会以 2 的恒定幂优化模吗?

假设我有类似的东西:

x % 32通常会被大多数 C/C++ 编译器(如 GCC)减少到吗x & 31