标签: compile-time-constant

Java - 静态初始化

我写了一段代码:

public class Child{
int y ;
private static final int z = getZ();

static {
    System.out.println("The value of z is "+z);
}

public int getX(){
    System.out.println("get x");
    return 10;
}

public int getY(){
    Child ch = new Child();
    System.out.println("get y");
    ch.y = getX();
    return y;
}

public static int getZ(){
    System.out.println("get z");
    return new Child().getY();
}

public Child(){
    System.out.println("Child constructor");
}


public static void main(String...args){
    Child ch = new Child();
    System.out.println("the value of z in main is "+z);
} …
Run Code Online (Sandbox Code Playgroud)

java static compile-time-constant static-initialization

1
推荐指数
2
解决办法
764
查看次数

字符串文字数组中字符串文字的编译时大小

我正在寻找编译时 C 字符串文字的长度。给出定义:

static const char * const   header_left[] =
{
    "         |          |  Raw  |  Raw  |",
    "         |          | Start |  End  |",
    "Interval#| Duration | Point | Point |",
    "---------+----------+-------+-------+",
};
const unsigned int  rows_in_header = sizeof(header_left) / sizeof(header_left[0]);
Run Code Online (Sandbox Code Playgroud)

如何header_left[2]在不使用 的情况下找到字符串文字的长度strlen

在这个问题中,Determining the Length of a String Literal,有一条注释将数组声明为header_left[][4]。我不喜欢使用这种声明,因为字符串的数量有可能在不改变数量常量的情况下发生变化。我喜欢让编译器计算字符串的数量(参见定义rows_in_header)和每个字符串的长度。

这是针对嵌入式系统的,字符串被块写入串行端口。串口函数将指向数据的指针和数据的长度作为参数。串行端口代码针对块写入进行了优化。首选不使用,strlen因为这会浪费性能时间。

我在 ARM7TDMI 平台上使用 C99 和 IAR Embedded Workshop。
我包含了该c++标签,因为这也涉及 C++,我们将在首次产品发布后将代码迁移到 C++。

c c++ embedded string-literals compile-time-constant

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

将时间常量编译为浮点数

在将具有双精度的文件转换为浮点数后,编译器(MSCV2005)会警告代码常量中的转换doublefloatfor

...
float r;

r = (q1 + q2) * 0.5;
...
Run Code Online (Sandbox Code Playgroud)

即使q1, q2两个花车0.5似乎都被视为double.

如何调整这种行为,以便所有代码内常量都被视为floats?

c floating-point compile-time-constant

1
推荐指数
2
解决办法
81
查看次数

这个比较会被编译成泛型类中的常量布尔值吗?

typeof(T) == typeof(string)where T 是泛型类型参数编译为常量布尔值,因为条件在编译时是可知的?

c# generic-type-argument compile-time-constant compiler-optimization

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

是否可以在编译时从环境变量初始化变量?

我想在编译期间初始化一个变量。例如,我想初始化变量VAR,以VALUE编译代码时:

match env::var("VAR") {
    Ok(value) => println!("Ok {}", value),
    Err(e) => println!("Error ({})", e),
};
Run Code Online (Sandbox Code Playgroud)

但是,我想在no_std上下文中执行此操作,因此无法用于std::env访问环境。是否有可能做到这一点?

compile-time-constant rust

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

constexpr在函数调用中不起作用/应用

我已经实现了一个constexpr编译时哈希函数,如果被调用,它可以正常工作(即在编译时进行评估)

constexpr auto hash = CompileTimeHash( "aha" );
Run Code Online (Sandbox Code Playgroud)

但是我需要在实际代码中使用它作为函数的参数

foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr
Run Code Online (Sandbox Code Playgroud)

由于某个特定原因,我不能使用长版本

constexpr auto hash = CompileTimeHash( "aha" );
foo( hash );
Run Code Online (Sandbox Code Playgroud)

编译器(VC++)在短(第一)情况下不会编译时哈希.有没有办法实现这个目标?

编辑:现在可以找到一个覆盖3个案例的例子:https: //godbolt.org/z/JGAyuE 只有gcc在所有3个案例中完成它

c++ compile-time-constant compile-time constexpr c++17

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

自定义 strtoi 函数编译时问题

我正在尝试实现一个非常简单的strtoi功能。当动态创建传递的参数时,它工作正常(下面的情况 2)。但是,如果char[]在编译时分配的 char 是由which创建的,我会得到一个常量值,len因为std::strlen(ch)这会扰乱我的逻辑,我找不到解决方案。

 int strtoi(char* ch)
 {   
  int sum {0};
  int len = static_cast<int>(std::strlen(ch));
  int skipLast = static_cast<int>(static_cast<int>(ch[0]) == 45);

  //Integer Method
  for(int i = len - 1; i >= 0 + skipLast; i--)
  {
    if(static_cast<int>(ch[i]) < 48 || static_cast<int>(ch[i]) > 57)
      return 0;
    sum += (ch[i]-48) * std::pow(10,i-skipLast);
  }

   sum = skipLast == 1 ? -sum : sum;
   return sum;
 }

int main()
{
  char pos[3] {'1','2','3'};
  std::cout << strtoi(pos) …
Run Code Online (Sandbox Code Playgroud)

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

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

为什么它不是编译时间常数?

final int i=10;
final String s=new String("lkj");
Run Code Online (Sandbox Code Playgroud)

为什么第一个是编译时间常量但是第二个不是编译时间常量,即使final也与第二个一起使用?

java compile-time-constant

-2
推荐指数
1
解决办法
419
查看次数