标签: compile-time-constant

"int size = 10;" 产生一个恒定的表达?

以下代码在gcc 4.8和Clang 3.2下编译:

int main()
{
  int size = 10;
  int arr[size];
}
Run Code Online (Sandbox Code Playgroud)

C++标准的8.3.4/1表示数组的大小必须是一个整数常量表达式,这size似乎不是.这是两个编译器中的错误,还是我错过了什么?

最新的VC++ CTP用这个有趣的消息拒绝代码:

error C2466: cannot allocate an array of constant size 0
Run Code Online (Sandbox Code Playgroud)

有趣的是,它似乎认为size是零.但至少它拒绝了代码.gcc和Clang应该不一样吗?

c++ arrays compile-time-constant variable-length-array c++11

13
推荐指数
1
解决办法
1209
查看次数

在C++ 11之前的"常量表达式"

constexpr关键字是在C++ 11中引入的,因为(我认为)是"常量表达式"的相应概念.但是,这个概念隐含在C++ 98/c ++ 03中,因为数组声明需要一个常量表达式:

// valid:
int a[sizeof(int)];
int b[3+7];
int c[13/4];
const int n = 3;
int d[n];
// invalid:
int m = 4;
int e[m];
Run Code Online (Sandbox Code Playgroud)

还有其他"常量表达式",即可以在编译时评估(和/或必须)的表达式; 一个例子是模板参数.

对于预C++ 11,在C++ 98/03标准或其他地方是否存在以下情况?

  • 需要常量表达式的语法结构的完整列表(例如数组声明和模板实例化)
  • 管理这种常量表达式的规则(可能这只是从上面列表中的项到它们在标准中的定义的映射)

c++ compile-time-constant constant-expression c++03 c++98

13
推荐指数
1
解决办法
1409
查看次数

Compiletime构建了std :: regex

std::regex在我的项目中工作.由于我在编译时知道正则表达式,并且构建正则表达式是在O(2 ^ m)中,其中m是正则表达式的长度,我希望在编译时建立正则表达式.这可能与std :: regex一起吗?(我不这么认为,因为我没有看到任何constexpr ctor basic_regex)如果没有,那么有一个正则表达式库可以在编译时构建我的正则表达式

c++ regex compile-time-constant c++11

13
推荐指数
2
解决办法
5056
查看次数

在 constexpr 构造函数中初始化数组是否合法?

下面的代码合法吗?

template <int N>
class foo {
public:
    constexpr foo()
    {
        for (int i = 0; i < N; ++i) {
            v_[i] = i;
        }
    }

private:
    int v_[N];
};

constexpr foo<5> bar;
Run Code Online (Sandbox Code Playgroud)

Clang 接受它,但 GCC 和 MSVC 拒绝它。

GCC 的错误是:

main.cpp:15:18: error: 'constexpr foo<N>::foo() [with int N = 5]' called in a constant expression
   15 | constexpr foo<5> bar;
      |                  ^~~
main.cpp:4:15: note: 'constexpr foo<N>::foo() [with int N = 5]' is not usable as a 'constexpr' function because:
    4 …
Run Code Online (Sandbox Code Playgroud)

c++ compile-time-constant template-meta-programming constexpr

13
推荐指数
1
解决办法
839
查看次数

为什么Java常量除以零会产生编译时错误?

可能重复:
1/0是合法的Java表达式吗?

为什么这段代码会编译?

class Compiles {
    public final static int A = 7/0;
    public final static int B = 10*3;

    public static void main(String[] args) {}
}
Run Code Online (Sandbox Code Playgroud)

如果我查看已编译的类文件,我可以看到B已经被评估为30,而A仍然是7/0.

据我所知,JSL是一个除以零的表达式,它不是常数.

参考:JLS 15.28

我的上述陈述是由于这一行:

编译时常量表达式是表示基本类型值的表达式

因此,除以零不会被评估为原始值.

我真的不明白为什么编译器允许这样做呢?为了清楚起见,上面的代码使用"java.lang.ExceptionInInitializerError"崩溃了运行时

在我看来,编译器威胁任何最终的静态变量作为常量并评估它的编译时间.这意味着编译器已经尝试评估A,但由于它是零除以它只是让它通过.没有编译时错误.但这看起来非常奇怪...编译器知道它是零除以它会崩溃运行时但是它不会标记编译错误!

任何人都可以向我解释原因吗?

java constants compile-time-constant jls constant-expression

12
推荐指数
1
解决办法
3753
查看次数

在C++编译时没有内置的计算能力的方法吗?

我有以下非常简单的模板.据我所知,^不是指数运算符.现在我正在寻找一种计算这种能力的方法.互联网上有许多递归模板的例子.这不是太难.

但我想知道:在C++中实际上没有"内置"方法来在编译时计算它吗?

template <int DIM>
class BinIdx : Idx
{
        static const int SIZE = 3 ^ DIM; // whoops, this is NOT an exponential operator!
}
Run Code Online (Sandbox Code Playgroud)

c++ templates compile-time-constant

12
推荐指数
4
解决办法
6007
查看次数

我可以在编译时在c中计算pow(10,x)吗?

是否有可能在编译时计算pow(10,x)?

我有一个没有浮点支持和慢整数除法的处理器.我正在尝试在编译时执行尽可能多的计算.如果我传递两个xC/pow(10,x)作为参数(x和C总是常量整数,但它们是每个调用的不同常量),我可以大大加速一个特定的函数.我想知道我是否可以通过引入一个1/pow(10,x)自动执行的宏来使这些函数调用不易出错,而不是强迫程序员计算它?

有预处理器技巧吗?我可以强制编译器优化库调用吗?

c compile-time-constant

11
推荐指数
5
解决办法
2万
查看次数

如何在类的头文件中定义const double?

在我的类的头文件中,我正在尝试以下并获得编译器投诉:

private:
    static const double some_double= 1.0;
Run Code Online (Sandbox Code Playgroud)

你怎么这么做呢?

c++ const static-members compile-time-constant

11
推荐指数
1
解决办法
1万
查看次数

基于编译时间值的c ++模板专业化

我很想进入模板元编程,慢慢地,我不知道如何实现以下内容:

// hpp file
enum MyEnum { Alive = 0, Dead };
class A {
    public:
        template<typename T, typename O, MyEnum ls>
        static int Register();
};

// elsewhere in the code...
A::Register<IType1, Type1, Dead>();
Run Code Online (Sandbox Code Playgroud)

在编译时,我将知道第三个模板类型的枚举值(编译时不变量),死亡或活动.是否可以为Register函数定义两个实体,如:

// desired hpp file
template<typename T, typename O, Alive>
int Register();

template<typename T, typename O, Dead>
int Register();

// corresponding desired .inc file
template<typename T, typename O, Alive>
int Register() { // Alive specific implementation ...  }

template<typename T, typename O, Dead>
int Register() { …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming compile-time-constant

11
推荐指数
1
解决办法
4095
查看次数

编译时数组常量

我似乎错过了一些相当基本的东西.我正在尝试在编译时使用const数组成员.

const int list[3] = { 2, 5, 7 };
const int a = list[2]; // this doesn't error?

template<int N1, int N2>
struct tmax {
  enum { value = ((N1 > N2) ? N1 : N2) };
};

const int b = tmax<2,4>::value;
const int c = tmax<list[0],list[1]>::value; // error is here

int main()
{
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

prog.cpp:10:24: error: 'list' cannot appear in a constant-expression
prog.cpp:10:30: error: an array reference cannot appear in a constant-expression
Run Code Online (Sandbox Code Playgroud)

这是相关的IDEOne链接 …

c++ standards templates compile-time-constant

11
推荐指数
1
解决办法
2726
查看次数