求助关于constexpr
  • 板块学术版
  • 楼主zhangbo1000
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/5/10 13:13
  • 上次更新2024/5/10 17:38:41
查看原帖
求助关于constexpr
760291
zhangbo1000楼主2024/5/10 13:13

有如下两段代码:

	constexpr double pi2=2*acos(-1);
	class MakeW{
		friend cp;
	public:
      // cp 是前面定义的一个类,有 a,b 两个成员变量
		cp w[20+1][(1<<20)/4];
		constexpr MakeW(){
			for(int i=0;i<(1<<20)/4;i++){
				w[20][i].a=cos(pi2*i/(1<<20));
				w[20][i].b=sin(pi2*i/(1<<20));
			}
			for(int i=20-1;i>0;i--){
				for(int j=0;j<(1<<i)/4;j++){
					w[i][j].a=w[i+1][j<<1].a;
					w[i][j].b=w[i+1][j<<1].b;
				}
			}
		}
	};
constexpr MakeW w;

可以正常运行。


	constexpr double pi2=2*acos(-1);
    class MakeW{
		friend cp;
        public:
		cp w1[std::max((1<<1)/4,1)]
		,w2[std::max((1<<2)/4,1)]
		,w3[std::max((1<<3)/4,1)]
		,w4[std::max((1<<4)/4,1)]
		,w5[std::max((1<<5)/4,1)]
		,w6[std::max((1<<6)/4,1)]
		,w7[std::max((1<<7)/4,1)]
		,w8[std::max((1<<8)/4,1)]
		,w9[std::max((1<<9)/4,1)]
		,w10[std::max((1<<10)/4,1)]
		,w11[std::max((1<<11)/4,1)]
		,w12[std::max((1<<12)/4,1)]
		,w13[std::max((1<<13)/4,1)]
		,w14[std::max((1<<14)/4,1)]
		,w15[std::max((1<<15)/4,1)]
		,w16[std::max((1<<16)/4,1)]
		,w17[std::max((1<<17)/4,1)]
		,w18[std::max((1<<18)/4,1)]
		,w19[std::max((1<<19)/4,1)]
		,w20[std::max((1<<20)/4,1)];
		cp *w[21];
        constexpr MakeW():w{nullptr,w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,w17,w18,w19,w20}{
			for(int i=0;i<(1<<20)/4;i++){
				w[20][i].a=cos(pi2*i/(1<<20));
				w[20][i].b=sin(pi2*i/(1<<20));
			}
			for(int i=19;i>0;i--){
				for(int j=0;j<(1<<i)/4;j++){
					w[i][j].a=w[i+1][j<<1].a;
					w[i][j].b=w[i+1][j<<1].b;
				}
			}
        }
    };

会 CE:

'constexpr' evaluation operation count exceeds limit of 33554432 (use '-fconstexpr-ops-limit=' to increase the limit)

但实际上,一来运算量明显不足 3355443233554432 的界限,而且第二份代码(就我看来)只是为了省内存而修改了 ww 的定义方式,请问这是为什么?

2024/5/10 13:13
加载中...