2012-01-06 14 views
120

在最近的一次採訪中,我被問到一個非常奇怪的問題。面試官問我如何使用編譯器功能來計算1 + 2 + 3 + ... + 1000。這意味着我不允許編寫程序並執行它,但是我應該編寫一個程序,它可以驅動編譯器在編譯時計算總和,並在編譯完成時輸出結果。作爲暗示,他告訴我可以使用編譯器的泛型和預處理器功能。可以使用C++,C#或Java編譯器。有任何想法嗎???如何驅動C#,C++或Java編譯器計算1 + 2 + 3 + ... + 1000?

此問題與計算沒有任何循環的總和asked here無關。另外,應該注意的是,總和應該在編譯期間計算。僅使用C++編譯器指令打印結果是不可接受的。


閱讀更多關於公佈答案,我發現,解決在使用C++模板編譯過程中出現問題被稱爲元編程。這是Erwin Unruh博士在標準化C++語言的過程中偶然發現的一種技術。您可以在wiki page of meta-programming上閱讀有關此主題的更多信息。 似乎可以使用Java註釋在Java中編寫程序。你可以看看下面的maress's答案。

一本關於C++元編程的好書是this one。值得一看,如果感興趣。

一個有用的C++元編程庫是Boost的MPL this link

+17

#error「500500」編譯錯誤是否計爲「完成」? – Mysticial 2012-01-06 19:43:27

+4

提示本質上意味着您使用C++模板。顯然不一樣,但這一個是打印1到1000,我相信你可以修改它以增加到一千... http://stackoverflow.com/questions/4568645/printing-1-to-1000-無環或條件 – Joe 2012-01-06 19:43:29

+7

'const int value = 1 + 2 + 3 .... + 1000;安慰。WriteLine(value);'; P – 2012-01-06 19:44:15

回答

114

更新現在改進了遞歸深度!適用於MSVC10和GCC,不增加深度。 :)


簡單的編譯時間遞歸+除了:

template<unsigned Cur, unsigned Goal> 
struct adder{ 
    static unsigned const sub_goal = (Cur + Goal)/2; 
    static unsigned const tmp = adder<Cur, sub_goal>::value; 
    static unsigned const value = tmp + adder<sub_goal+1, Goal>::value; 
}; 

template<unsigned Goal> 
struct adder<Goal, Goal>{ 
    static unsigned const value = Goal; 
}; 

Testcode:

template<unsigned Start> 
struct sum_from{ 
    template<unsigned Goal> 
    struct to{ 
    template<unsigned N> 
    struct equals; 

    typedef equals<adder<Start, Goal>::value> result; 
    }; 
}; 

int main(){ 
    sum_from<1>::to<1000>::result(); 
} 

輸出爲GCC:

error: declaration of ‘struct sum_from<1u>::to<1000u>::equals<500500u>’

Live example on Ideone

輸出爲MSVC10:

error C2514: 'sum_from<Start>::to<Goal>::equals<Result>' : class has no constructors 
     with 
     [ 
      Start=1, 
      Goal=1000, 
      Result=500500 
     ] 
+0

@hsalimi:我編輯了答案,實際顯示了一些完成工作的代碼。 :) – Xeo 2012-01-06 20:06:29

+0

哇,你真的給我留下了深刻的印象:-) – hsalimi 2012-01-06 20:09:34

+0

@hsalimi:Erwin Unruh博士在斯德哥爾摩於1997年C++標準化會議上發明了這項技術。他計算了一系列素數。 – 2012-01-06 20:46:56

2

你可以使用(並且主要是濫用)C++宏/模板來做metaprogramming。 AFAIK,Java不允許有同樣的事情。

+0

+1感謝您的鏈接。這似乎很有幫助。 – hsalimi 2012-01-06 19:58:58

+2

這不是問題的答案。 – Ikke 2012-01-07 14:33:46

+0

我認爲你是對的。在Java中,你不能使用相同的模板遞歸技巧,因爲泛型類參數不能是一個值 - 它必須是一個類。 – 2012-01-07 22:17:33

1

從理論上講,你可以使用這個:

#include <iostream> 

template<int N> 
struct Triangle{ 
    static int getVal() 
    { 
    return N + Triangle<N-1>::getVal(); 
    } 
}; 

template<> 
struct Triangle<1>{ 
    static int getVal() 
    { 
    return 1; 
    } 
}; 

int main(){ 
    std::cout << Triangle<1000>::getVal() << std::endl; 
    return 0; 
} 

(基於該XEO發佈的代碼)。但GCC給我這個錯誤:

triangle.c++:7: error: template instantiation depth exceeds maximum of 500 (use -ftemplate-depth-NN to increase the maximum) instantiating struct Triangle<500>

加上一個巨大的僞堆棧跟蹤。

+0

必須使用標誌:-template-depth-1000 – Jetti 2012-01-06 19:57:55

+0

@Jetti:好的,是的;這就是錯誤信息所說的。 :-) – ruakh 2012-01-06 20:01:28

+0

+1它對低於500的數字起作用,例如450? – hsalimi 2012-01-06 20:02:49

49

I should just write a program that could drive the compiler to compute this sum while compilation and print the result when compilation completes.

一個招人喜愛編譯期間打印數量正試圖訪問與數實例化要打印模板的一個不存在的成員:

template<int> struct print_n {}; 

print_n<1000 * 1001/2>::foobar go; 

編譯器然後說:

error: 'foobar' in 'struct print_n<500500>' does not name a type 

有關此技術的更有趣的示例,請參閱Solve the eight queens problem at compile-time

+0

+1確實很漂亮 – 2012-01-06 20:00:44

+0

+1謝謝。尼斯認爲。 :-)但它不計算總和。 – hsalimi 2012-01-06 20:01:46

+0

您可以讓'print_n'保持未定義狀態,請參閱我的答案。 – Xeo 2012-01-06 20:04:05

88

C#示例在編譯時出錯。

class Foo 
{ 
    const char Sum = (1000 + 1) * 1000/2; 
} 

產生如下編譯錯誤:

Constant value '500500' cannot be converted to a 'char' 
+3

@ildjarn好的,C++模板的答案和這一個是有區別的:這裏只適用於常量摺疊,而模板允許任意(?)代碼。把它分配給char也是一個好主意! – Voo 2012-01-06 20:44:30

+0

@Voo是的,但公平的C#只是沒有比較這種編程與C++。 – Marlon 2012-01-06 21:14:35

+2

@Marion我真的不認爲語言設計中存在一個錯誤;)模板元編程可能是非常強大的,但其他語言仍然可以用其他解決方案完成大部分工作,而這些解決方案不具備所有這些缺陷。我不得不花費數小時才能編譯的項目(並非完全正確 - 如果我們沒有增加遞歸實例化極限,它在幾秒鐘內就失敗了,它的速度非常快),並且很難維護。可能是我不太喜歡它的原因.. – Voo 2012-01-06 22:16:23

7

下面是在VC++ 2010工程的實施,我不得不向上突破的計算分爲3個階段,因爲編譯器抱怨,當模板遞歸500+次。

template<int t_startVal, int t_baseVal = 0, int t_result = 0> 
struct SumT 
{ 
    enum { result = SumT<t_startVal - 1, t_baseVal, t_baseVal + t_result + 
     t_startVal>::result }; 
}; 

template<int t_baseVal, int t_result> 
struct SumT<0, t_baseVal, t_result> 
{ 
    enum { result = t_result }; 
}; 

template<int output_value> 
struct Dump 
{ 
    enum { value = output_value }; 
    int bad_array[0]; 
}; 

enum 
{ 
    value1 = SumT<400>::result,    // [1,400] 
    value2 = SumT<400, 400, value1>::result, // [401, 800] 
    value3 = SumT<200, 800, value2>::result // [801, 1000] 
}; 

Dump<value3> dump; 

當你編譯這個,你應該看到從編譯器像這樣的輸出:

1>warning C4200: nonstandard extension used : zero-sized array in struct/union 
1>   Cannot generate copy-ctor or copy-assignment operator when UDT contains a 
zero-sized array 
1>   templatedrivensum.cpp(33) : see reference to class template 
instantiation 'Dump<output_value>' being compiled 
1>   with 
1>   [ 
1>    output_value=500500 
1>   ] 
+0

非常好的想法與分解,我想我會以某種方式將它納入我的答案。 +1 :) – Xeo 2012-01-07 01:09:29

+0

+1尼斯......謝謝。 – hsalimi 2012-01-09 11:42:37

30

由於沒有編譯器和語言在面試中的問題進行了規定,我敢提出在Haskell的解決方案使用GHC:

{-# LANGUAGE TemplateHaskell #-} 
{-# OPTIONS_GHC -ddump-splices #-} 
module Main where 

main :: IO() 
main = print $(let x = sum [1 :: Int .. 1000] in [| x |]) 

編譯:

$ ghc compsum.hs 
[1 of 1] Compiling Main    (compsum.hs, compsum.o) 
Loading package ghc-prim ... linking ... done. 
<snip more "Loading package ..." messages> 
Loading package template-haskell ... linking ... done. 
compsum.hs:6:16-56: Splicing expression 
    let x = sum [1 :: Int .. 1000] in [| x |] ======> 500500 
Linking compsum ... 

我們也有一個工作程序。

18

使用C++ 11可以讓生活更輕鬆,它爲編譯時計算增加了constexpr函數,儘管它們目前僅支持gcc 4.6或更高版本。

constexpr unsigned sum(unsigned start, unsigned end) { 
    return start == end ? start : 
     sum(start, (start + end)/2) + 
     sum((start + end)/2 + 1, end); 
} 

template <int> struct equals; 
equals<sum(1,1000)> x; 

該標準只要求編譯器支持512的遞歸深度,所以它仍然需要避免線性遞歸深度。下面是輸出:

$ g++-mp-4.6 --std=c++0x test.cpp -c 
test.cpp:8:25: error: aggregate 'equals<500500> x' has incomplete type and cannot be defined 

當然你也可以只使用公式:

constexpr unsigned sum(unsigned start, unsigned end) { 
    return (start + end) * (end - start + 1)/2; 
} 

// static_assert is a C++11 assert, which checks 
// at compile time. 
static_assert(sum(0,1000) == 500500, "Sum failed for 0 to 1000"); 
+1

+1,完全忘了'constexpr'一會兒。也許我太喜歡模板了。 :( – Xeo 2012-01-07 08:30:53

+0

這是constexpr用於解決這個問題的一個很好用法(請參閱Adder實現):http://kaizer.se/wiki/log/post/C++_constexpr_foldr/ – Matt 2012-01-11 17:53:11

12

在java中,我想過用註釋處理。 在將源文件解析爲javac命令之前,apt工具會掃描源文件。

在源文件的彙編,輸出將被打印出來:

@Documented 
@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
public @interface MyInterface { 

    int offset() default 0; 

    int last() default 100; 
} 

處理器工廠:

​​3210

實際註釋處理器:

public class MyInterfaceAnnotationProcessor implements AnnotationProcessor { 

    private AnnotationProcessorEnvironment ape; 
    private AnnotationTypeDeclaration atd; 

    public MyInterfaceAnnotationProcessor(AnnotationProcessorEnvironment ape) { 
     this.ape = ape; 
     atd = (AnnotationTypeDeclaration) ape.getTypeDeclaration("practiceproject.MyInterface"); 
    } 

    public void process() { 
     Collection<Declaration> decls = ape.getDeclarationsAnnotatedWith(atd); 
     for (Declaration dec : decls) { 
      processDeclaration(dec); 
     } 
    } 

    private void processDeclaration(Declaration d) { 
     Collection<AnnotationMirror> ams = d.getAnnotationMirrors(); 
     for (AnnotationMirror am : ams) { 
      if (am.getAnnotationType().getDeclaration().equals(atd)) { 
       Map<AnnotationTypeElementDeclaration, AnnotationValue> values = am.getElementValues(); 
       int offset = 0; 
       int last = 100; 
       for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : values.entrySet()) { 
        AnnotationTypeElementDeclaration ated = entry.getKey(); 
        AnnotationValue v = entry.getValue(); 
        String name = ated.getSimpleName(); 
        if (name.equals("offset")) { 
         offset = ((Integer) v.getValue()).intValue(); 
        } else if (name.equals("last")) { 
         last = ((Integer) v.getValue()).intValue(); 
        } 
       } 
       //find the sum 
       System.err.println("Sum: " + ((last + 1 - offset)/2) * (2 * offset + (last - offset))); 
      } 
     } 
    } 
} 

然後我們創建一個源文件。

@MyInterface(offset = 1, last = 1000) 
public class Main { 

    @MyInterface 
    void doNothing() { 
     System.out.println("Doing nothing"); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Main m = new Main(); 
     m.doNothing(); 
     MyInterface my = (MyInterface) m.getClass().getAnnotation(MyInterface.class); 
     System.out.println("offset: " + my.offset()); 
     System.out.println("Last: " + my.last()); 
    } 
} 

註釋處理器被編譯成一個jar文件,則易於工具用於編譯源文件爲::使用MyInterface的註釋簡單的類

apt -cp "D:\Variance project\PracticeProject\dist\practiceproject.jar" -factory practiceproject.annotprocess.MyInterfaceAnnotationProcessorFactory "D:\Variance project\PracticeProject2\src\practiceproject2\Main.java" 

該項目的輸出:

Called supportedAnnotationTypes........................... 
Called getProcessorFor................ 
Sum: 5000 
Sum: 500500 
+0

+1 Wowww ....似乎有趣:-) – hsalimi 2012-01-11 08:36:43

7

我覺得有義務給這個C代碼,因爲沒有其他人尚未:

#include <stdio.h> 
int main() { 
    int x = 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+ 
      21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+ 
      41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+ 
      61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+ 
      81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100+  
      101+102+103+104+105+106+107+108+109+110+111+112+113+114+115+116+117+118+119+120+ 
      121+122+123+124+125+126+127+128+129+130+131+132+133+134+135+136+137+138+139+140+ 
      141+142+143+144+145+146+147+148+149+150+151+152+153+154+155+156+157+158+159+160+ 
      161+162+163+164+165+166+167+168+169+170+171+172+173+174+175+176+177+178+179+180+ 
      181+182+183+184+185+186+187+188+189+190+191+192+193+194+195+196+197+198+199+200+ 
      201+202+203+204+205+206+207+208+209+210+211+212+213+214+215+216+217+218+219+220+ 
      221+222+223+224+225+226+227+228+229+230+231+232+233+234+235+236+237+238+239+240+ 
      241+242+243+244+245+246+247+248+249+250+251+252+253+254+255+256+257+258+259+260+ 
      261+262+263+264+265+266+267+268+269+270+271+272+273+274+275+276+277+278+279+280+ 
      281+282+283+284+285+286+287+288+289+290+291+292+293+294+295+296+297+298+299+300+ 
      301+302+303+304+305+306+307+308+309+310+311+312+313+314+315+316+317+318+319+320+ 
      321+322+323+324+325+326+327+328+329+330+331+332+333+334+335+336+337+338+339+340+ 
      341+342+343+344+345+346+347+348+349+350+351+352+353+354+355+356+357+358+359+360+ 
      361+362+363+364+365+366+367+368+369+370+371+372+373+374+375+376+377+378+379+380+ 
      381+382+383+384+385+386+387+388+389+390+391+392+393+394+395+396+397+398+399+400+ 
      401+402+403+404+405+406+407+408+409+410+411+412+413+414+415+416+417+418+419+420+ 
      421+422+423+424+425+426+427+428+429+430+431+432+433+434+435+436+437+438+439+440+ 
      441+442+443+444+445+446+447+448+449+450+451+452+453+454+455+456+457+458+459+460+ 
      461+462+463+464+465+466+467+468+469+470+471+472+473+474+475+476+477+478+479+480+ 
      481+482+483+484+485+486+487+488+489+490+491+492+493+494+495+496+497+498+499+500+ 
      501+502+503+504+505+506+507+508+509+510+511+512+513+514+515+516+517+518+519+520+ 
      521+522+523+524+525+526+527+528+529+530+531+532+533+534+535+536+537+538+539+540+ 
      541+542+543+544+545+546+547+548+549+550+551+552+553+554+555+556+557+558+559+560+ 
      561+562+563+564+565+566+567+568+569+570+571+572+573+574+575+576+577+578+579+580+ 
      581+582+583+584+585+586+587+588+589+590+591+592+593+594+595+596+597+598+599+600+ 
      601+602+603+604+605+606+607+608+609+610+611+612+613+614+615+616+617+618+619+620+ 
      621+622+623+624+625+626+627+628+629+630+631+632+633+634+635+636+637+638+639+640+ 
      641+642+643+644+645+646+647+648+649+650+651+652+653+654+655+656+657+658+659+660+ 
      661+662+663+664+665+666+667+668+669+670+671+672+673+674+675+676+677+678+679+680+ 
      681+682+683+684+685+686+687+688+689+690+691+692+693+694+695+696+697+698+699+700+ 
      701+702+703+704+705+706+707+708+709+710+711+712+713+714+715+716+717+718+719+720+ 
      721+722+723+724+725+726+727+728+729+730+731+732+733+734+735+736+737+738+739+740+ 
      741+742+743+744+745+746+747+748+749+750+751+752+753+754+755+756+757+758+759+760+ 
      761+762+763+764+765+766+767+768+769+770+771+772+773+774+775+776+777+778+779+780+ 
      781+782+783+784+785+786+787+788+789+790+791+792+793+794+795+796+797+798+799+800+ 
      801+802+803+804+805+806+807+808+809+810+811+812+813+814+815+816+817+818+819+820+ 
      821+822+823+824+825+826+827+828+829+830+831+832+833+834+835+836+837+838+839+840+ 
      841+842+843+844+845+846+847+848+849+850+851+852+853+854+855+856+857+858+859+860+ 
      861+862+863+864+865+866+867+868+869+870+871+872+873+874+875+876+877+878+879+880+ 
      881+882+883+884+885+886+887+888+889+890+891+892+893+894+895+896+897+898+899+900+ 
      901+902+903+904+905+906+907+908+909+910+911+912+913+914+915+916+917+918+919+920+ 
      921+922+923+924+925+926+927+928+929+930+931+932+933+934+935+936+937+938+939+940+ 
      941+942+943+944+945+946+947+948+949+950+951+952+953+954+955+956+957+958+959+960+ 
      961+962+963+964+965+966+967+968+969+970+971+972+973+974+975+976+977+978+979+980+ 
      981+982+983+984+985+986+987+988+989+990+991+992+993+994+995+996+997+998+999+1000; 
    printf("%d\n", x); 
} 

所有我需要做的是檢查裝配找到我的答案!

gcc -S compile_sum.c; 
grep "\$[0-9]*, *-4" compile_sum.s 

而且我看到:

movl $500500, -4(%rbp) 
+0

特定實現的功能,而不是C語言。 – Puppy 2012-07-21 01:46:56

+3

你知道多少** C編譯器**不是C的「特定實現」? – 2012-07-21 23:18:11

+0

注意:這種方法適用於大多數語言。 – 2013-02-24 12:43:45

1

使用Java中,你可以做類似的事情在C#的答案:

public class Cheat { 
    public static final int x = (1000 *1001/2); 
} 

javac -Xprint Cheat.java 

public class Cheat { 

    public Cheat(); 
    public static final int x = 500500; 
} 

您可以在scala using peano numbers做到這一點,因爲你可以強制編譯器做遞歸,但我不認爲你可以做同樣的事情在C#/ java的

anoth er解決方案不使用-Xprint,但更狡猾

public class Cheat { 
    public static final int x = 5/(1000 *1001/2 - 500500); 
} 

javac -Xlint:all Cheat.java 

Cheat.java:2: warning: [divzero] division by zero 
    public static final int x = 5/(1000 *1001/2 - 500500); 
          ^
1 warning 

沒有使用任何編譯器標誌。既然你可以檢查任意數量的常量(不只是500500),這個解決方案應該是可以接受的。

public class Cheat { 
    public static final short max = (Short.MAX_VALUE - 500500) + 1001*1000/2; 
    public static final short overflow = (Short.MAX_VALUE - 500500 + 1) + 1001*1000/2; 

} 

Cheat.java:3: error: possible loss of precision 
    public static final short overflow = (Short.MAX_VALUE - 500500 + 1) + 1001*1000/2; 
                   ^
    required: short 
    found: int 
1 error 
+0

抱歉,您沒有將編譯器驅動到* compute *'500500'。 – Xeo 2012-08-12 17:38:53

+0

這是參考所有三種解決方案嗎? 在解決方案1我拿了一些java代碼並編譯它,編譯器打印出500500.這看起來很像編譯器計算500500.那怎麼不是編譯器計算500500? – benmmurphy 2012-08-12 17:49:33

+0

是的,真的夠了,我在談論解決方案2和3.我在早先的更新中已經閱讀了這個答案,並且回到了最新的答案上,似乎忘記了第一個解決方案。 – Xeo 2012-08-12 17:51:12

4

從卡爾·沃爾什的回答擴展到編譯過程中實際打印結果:

#define VALUE (1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+\ 
21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+\ 
41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+\ 
61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+\ 
81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100+\ 
101+102+103+104+105+106+107+108+109+110+111+112+113+114+115+116+117+118+119+120+\ 
121+122+123+124+125+126+127+128+129+130+131+132+133+134+135+136+137+138+139+140+\ 
141+142+143+144+145+146+147+148+149+150+151+152+153+154+155+156+157+158+159+160+\ 
161+162+163+164+165+166+167+168+169+170+171+172+173+174+175+176+177+178+179+180+\ 
181+182+183+184+185+186+187+188+189+190+191+192+193+194+195+196+197+198+199+200+\ 
201+202+203+204+205+206+207+208+209+210+211+212+213+214+215+216+217+218+219+220+\ 
221+222+223+224+225+226+227+228+229+230+231+232+233+234+235+236+237+238+239+240+\ 
241+242+243+244+245+246+247+248+249+250+251+252+253+254+255+256+257+258+259+260+\ 
261+262+263+264+265+266+267+268+269+270+271+272+273+274+275+276+277+278+279+280+\ 
281+282+283+284+285+286+287+288+289+290+291+292+293+294+295+296+297+298+299+300+\ 
301+302+303+304+305+306+307+308+309+310+311+312+313+314+315+316+317+318+319+320+\ 
321+322+323+324+325+326+327+328+329+330+331+332+333+334+335+336+337+338+339+340+\ 
341+342+343+344+345+346+347+348+349+350+351+352+353+354+355+356+357+358+359+360+\ 
361+362+363+364+365+366+367+368+369+370+371+372+373+374+375+376+377+378+379+380+\ 
381+382+383+384+385+386+387+388+389+390+391+392+393+394+395+396+397+398+399+400+\ 
401+402+403+404+405+406+407+408+409+410+411+412+413+414+415+416+417+418+419+420+\ 
421+422+423+424+425+426+427+428+429+430+431+432+433+434+435+436+437+438+439+440+\ 
441+442+443+444+445+446+447+448+449+450+451+452+453+454+455+456+457+458+459+460+\ 
461+462+463+464+465+466+467+468+469+470+471+472+473+474+475+476+477+478+479+480+\ 
481+482+483+484+485+486+487+488+489+490+491+492+493+494+495+496+497+498+499+500+\ 
501+502+503+504+505+506+507+508+509+510+511+512+513+514+515+516+517+518+519+520+\ 
521+522+523+524+525+526+527+528+529+530+531+532+533+534+535+536+537+538+539+540+\ 
541+542+543+544+545+546+547+548+549+550+551+552+553+554+555+556+557+558+559+560+\ 
561+562+563+564+565+566+567+568+569+570+571+572+573+574+575+576+577+578+579+580+\ 
581+582+583+584+585+586+587+588+589+590+591+592+593+594+595+596+597+598+599+600+\ 
601+602+603+604+605+606+607+608+609+610+611+612+613+614+615+616+617+618+619+620+\ 
621+622+623+624+625+626+627+628+629+630+631+632+633+634+635+636+637+638+639+640+\ 
641+642+643+644+645+646+647+648+649+650+651+652+653+654+655+656+657+658+659+660+\ 
661+662+663+664+665+666+667+668+669+670+671+672+673+674+675+676+677+678+679+680+\ 
681+682+683+684+685+686+687+688+689+690+691+692+693+694+695+696+697+698+699+700+\ 
701+702+703+704+705+706+707+708+709+710+711+712+713+714+715+716+717+718+719+720+\ 
721+722+723+724+725+726+727+728+729+730+731+732+733+734+735+736+737+738+739+740+\ 
741+742+743+744+745+746+747+748+749+750+751+752+753+754+755+756+757+758+759+760+\ 
761+762+763+764+765+766+767+768+769+770+771+772+773+774+775+776+777+778+779+780+\ 
781+782+783+784+785+786+787+788+789+790+791+792+793+794+795+796+797+798+799+800+\ 
801+802+803+804+805+806+807+808+809+810+811+812+813+814+815+816+817+818+819+820+\ 
821+822+823+824+825+826+827+828+829+830+831+832+833+834+835+836+837+838+839+840+\ 
841+842+843+844+845+846+847+848+849+850+851+852+853+854+855+856+857+858+859+860+\ 
861+862+863+864+865+866+867+868+869+870+871+872+873+874+875+876+877+878+879+880+\ 
881+882+883+884+885+886+887+888+889+890+891+892+893+894+895+896+897+898+899+900+\ 
901+902+903+904+905+906+907+908+909+910+911+912+913+914+915+916+917+918+919+920+\ 
921+922+923+924+925+926+927+928+929+930+931+932+933+934+935+936+937+938+939+940+\ 
941+942+943+944+945+946+947+948+949+950+951+952+953+954+955+956+957+958+959+960+\ 
961+962+963+964+965+966+967+968+969+970+971+972+973+974+975+976+977+978+979+980+\ 
981+982+983+984+985+986+987+988+989+990+991+992+993+994+995+996+997+998+999+1000) 

char tab[VALUE]; 

int main() 
{ 
    tab = 5; 
} 

GCC輸出:

test.c: In function 'main': 
test.c:56:9: error: incompatible types when assigning to type 'char[500500]' fro 
m type 'int' 
0

雖然這實際上與小的數字作品,鐺++返回我一個編譯器如果我使用錯誤sum_first其中N> 400

#include <iostream> 

using namespace std; 


template <int N> 
struct sum_first 
{ 
    static const int value = N + sum_first<N - 1>::value; 
}; 

template <> 
struct sum_first<0> 
{ 
    static const int value = 0; 
}; 

int main() 
{ 
    cout << sum_first<1000>::value << endl; 
}