Re: [問題] 巨集中是否能對變數做前處理?
看板C_and_CPP (C/C++)作者loveflames (咕啾咕啾魔法陣)時間16年前 (2010/05/27 02:30)推噓0(0推 0噓 4→)留言4則, 2人參與討論串2/2 (看更多)
#include <iostream>
using namespace std;
#define max 6
#define a(b) A(b)
#define A(b) a##b
int m[10];
int a1=11,a2=12,a3=13,a4=14,a5=15,a6=16;
template<int n>
struct test
{
static void p(){
test<n-1>::p();
m[n]=*(&a(max)-(max-n));//這行執行時的結果可能會出錯
}
};
template<>
struct test<1>
{
static void p(){
m[1]=a1;
}
};
int main() {
test<max>::p();
for(int i=1;i<=max;i++)
cout<<"m["<<i<<"]="<<m[i]<<endl;
return 0;
}
-----------------------------
因為不知怎麼用template來產生a1~an這些名字
所以只好這樣寫
可是開最佳化後global變數就不是照順序放
紅色那行可能會有問題
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.134.226.149
→
05/27 04:00, , 1F
05/27 04:00, 1F
→
05/27 12:04, , 2F
05/27 12:04, 2F
→
05/27 12:08, , 3F
05/27 12:08, 3F
→
05/27 12:08, , 4F
05/27 12:08, 4F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
3
10
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章