如何驱动 C#、C++ 或 Java 编译器在编译时计算 1+2+3+...+1000?

How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?

提问人:TonySalimi 提问时间:1/7/2012 最后编辑:TonySalimi 更新时间:7/18/2018 访问量:9157

问:

在最近的一次采访中,我被问到一个非常奇怪的问题。面试官问我,仅使用编译器功能如何计算 1+2+3+...+1000。这意味着我不被允许编写程序并执行它,但我应该编写一个程序,该程序可以驱动编译器在编译时计算此总和,并在编译完成后打印结果。作为提示,他告诉我,我可能会使用编译器的泛型和预处理器功能。可以使用C++,C#或Java编译器。有什么想法吗???

这个问题与计算总和无关,这里没有问任何循环。此外,应该注意的是,总和应该在编译过程中计算。仅使用 C++ 编译器指令打印结果是不可接受的。


阅读有关发布答案的更多信息,我发现使用 C++ 模板在编译过程中解决问题称为元编程。这是Erwin Unruh博士在标准化C++语言的过程中偶然发现的一种技术。您可以在元编程的 wiki 页面上阅读有关此主题的更多信息。 似乎可以使用 java 注解在 Java 中编写程序。你可以看看下面 maress 的回答。

这是一本关于C++元编程的好书。如果有兴趣,值得一看。

一个有用的C++元编程库是Boost的MPL这个链接

C# Java C++ 编译器构造 元编程

评论

17赞 Mysticial 1/7/2012
#error “500500” 编译错误算不算“完成”?
4赞 Joe 1/7/2012
该提示实质上意味着您使用 C++ 模板。显然不一样,但这个是用于打印 1 到 1000 的,我相信您可以修改它以添加到一千......stackoverflow.com/questions/4568645/......
8赞 George Duckett 1/7/2012
const int value = 1 + 2 + 3.... + 1000; Console.WriteLine(value);;P
9赞 Chris 1/7/2012
有时我认为,有些面试问题只是为了证明面试官比面试官在智力上优于面试者。
4赞 Salman A 6/23/2012
在被问到这个问题之前,你要求很多钱吗?

答:

2赞 ptyx 1/7/2012 #1

您可以使用(主要是滥用)C++宏/模板来进行元编程。 AFAIK,Java 不允许同样的事情。

评论

2赞 Ikke 1/7/2012
不是这个问题的真正答案。
0赞 Eyal Schneider 1/8/2012
我认为你是对的。在 java 中,你不能使用相同的模板递归技巧,因为泛型类参数不能是值——它必须是一个类。
0赞 Allon Guralnek 1/11/2012
C# 编译器的泛型功能允许您执行一些编译时计算。请参阅 Eric Lippert 关于此的帖子
119赞 Xeo 1/7/2012 #2

更新现在改进了递归深度!适用于 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;
};

测试代码:

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 的输出:

错误:“结构sum_from<1U>::To<1000U>::Equals<500500U>”的声明

Ideone上的活生生的例子

MSVC10 的输出:

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

评论

0赞 Xeo 1/7/2012
@hsalimi:我编辑了答案,以实际显示一些完成工作的代码。:)
0赞 TonySalimi 1/7/2012
哇,你真的给我留下了深刻的印象:-)
0赞 Dietmar Kühl 1/7/2012
@hsalimi:1997 年在斯德哥尔摩举行的C++标准化会议上发明了这项技术,是 Erwin Unruh 博士发明的。他计算了一系列素数。
0赞 Adam Gritt 1/7/2012
为了使其在没有递归的情况下工作,您可以使用 N*(N+1)/2 的公式来计算总和。
2赞 AVH 1/12/2012
@hsalimi 如果你想在C++中看到更多精彩的模板元编程示例,我推荐 Andrei Alexandrescu 的 Modern C++ Design
1赞 ruakh 1/7/2012 #3

从理论上讲,您可以使用以下功能:

#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赞 Jetti 1/7/2012
必须使用标志:-ftemplate-depth-1000
0赞 ruakh 1/7/2012
@hsalimi:是的。它也适用于 1000,一旦你添加了标志。但是它没有在编译时打印,并且 Xeo 已经更改了他/她的答案以实际回答这个特定问题,所以我认为我的答案已经过时了。:-)
51赞 fredoverflow 1/7/2012 #4

我应该编写一个程序,该程序可以驱动编译器在编译时计算此总和,并在编译完成后打印结果。

在编译过程中打印数字的一个常用技巧是尝试访问使用要打印的数字实例化的模板中不存在的成员:

template<int> struct print_n {};

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

编译器接着说:

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

有关此技术的更有趣的示例,请参阅在编译时解决 eight queens 问题

评论

0赞 Xeo 1/7/2012
你也可以让保持未定义,看看我的答案。print_n
2赞 Daniel Fischer 1/7/2012
@David 但是高斯需要一种聪明的方法,他没有一台计算机来快速地完成它。
90赞 Marlon 1/7/2012 #5

编译时出错的 C# 示例。

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

产生以下编译错误:

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

评论

4赞 Voo 1/7/2012
@ildjarn 好吧,c++ 模板的答案和这个答案是有区别的:这里只有因为不断折叠而起作用,而模板允许任意 (?) 代码。将其分配给字符仍然是一个好主意!
0赞 Marlon 1/7/2012
@Voo 是的,但公平地说,C# 在这种编程方面无法与 C++ 相提并论。
3赞 Voo 1/7/2012
@Marion 我真的不认为这是语言设计中的错误;)模板元编程可能非常强大,但其他语言仍然可以用其他没有所有这些陷阱的解决方案来完成大部分事情。我不得不处理一个需要几个小时才能编译的项目(不完全正确 - 如果我们不增加递归实例化限制,它的速度快得令人难以置信......它在几秒钟内就失败了),而且维护起来很糟糕。这可能是我不太喜欢它的原因。
0赞 ildjarn 1/7/2012
@Voo : FredOverflow 的方法也依赖于不断折叠。至于编译缓慢,责怪你的编译器,而不是语言(提示 - Clang 编译 C++ 速度很快)。
0赞 Voo 1/7/2012
@ildjarn Clang 可以快速编译极其复杂、嵌套深度和极其复杂的模板?我认为一切皆有可能,我不能再测试它了(感谢上帝),但我无法想象。另外,我在这里谈论的是 Xeo 的方法,而不是 Fred 的方法。
9赞 hypercode 1/7/2012 #6

下面是在 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赞 Xeo 1/7/2012
分解它的好主意,我想我会以某种方式将其纳入我的答案中。+1 :)
31赞 Daniel Fischer 1/7/2012 #7

由于面试问题中既没有指定编译器也没有指定语言,因此我敢于使用 GHC 在 Haskell 中提交解决方案:

{-# 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 ...

我们还有一个工作计划。

20赞 Daniel James 1/7/2012 #8

使用 C++11 可以更轻松地工作,它添加了用于编译时计算的函数,尽管它们目前仅受 gcc 4.6 或更高版本的支持。constexpr

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赞 Xeo 1/7/2012
+1,一时完全忘记了。也许我只是太喜欢模板了。:(constexpr
0赞 Matt 1/12/2012
这是 constexpr 的一个很好的用法来解决这个问题(参见 Adder 实现): kaizer.se/wiki/log/post/C++_constexpr_foldr
0赞 Peter Cordes 3/2/2020
这个公式可能会溢出;最后一步是,为了处理所有可能的结果,右移的值必须是 N+1 位宽,但事实并非如此。可以重新排列公式以避免这种情况,就像 clang 对运行时变量范围所做的那样:godbolt.org/z/dUGXqg 表明 clang 知道闭合形式的公式并使用它来优化循环。/ 2unsignedtotal += i
14赞 maress 1/11/2012 #9

在 java 中,我考虑过使用注释处理。 apt 工具在实际将源文件解析为 javac 命令之前会扫描源文件。

在编译源文件期间,输出将被打印出来:

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

    int offset() default 0;

    int last() default 100;
}

处理器工厂:

public class MyInterfaceAnnotationProcessorFactory implements AnnotationProcessorFactory {

    public Collection<String> supportedOptions() {
        System.err.println("Called supportedOptions.............................");
        return Collections.EMPTY_LIST;
    }

    public Collection<String> supportedAnnotationTypes() {
        System.err.println("Called supportedAnnotationTypes...........................");
        return Collections.singletonList("practiceproject.MyInterface");
    }

    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> set, AnnotationProcessorEnvironment ape) {
        System.err.println("Called getProcessorFor................");
        if (set.isEmpty()) {
            return AnnotationProcessors.NO_OP;
        }
        return new MyInterfaceAnnotationProcessor(ape);
    }
}

实际的注解处理器:

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 注解的简单类:

 @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 文件,然后使用 apt 工具将源文件编译为:

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
9赞 Carl Walsh 7/21/2012 #10

我觉得有义务给出这个 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赞 Puppy 7/21/2012
特定实现的功能,而不是 C 语言。
5赞 Carl Walsh 7/22/2012
您知道有多少 C 编译器不是 C 的“特定实现”?
0赞 Peter Cordes 3/2/2020
@Puppy:如果是全局的,则编译器将(或多或少)需要在编译时计算表达式。ISO C 不允许全局变量初始值设定项。当然,特定的实现可以发出对类似构造函数的 static-init 函数的调用,该函数在运行时计算它并存储它。但是 ISO C 确实允许您使用编译时间常量作为数组大小(例如在结构定义中或作为另一个全局大小),因此任何假设的悲观实现仍然必须支持这一点。xint y[x];
1赞 benmmurphy 8/13/2012 #11

使用 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 中使用 Peano Numbers 来做到这一点,因为你可以强制编译器执行递归,但我认为你不能在 C#/Java 中做同样的事情

另一个不使用-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赞 Xeo 8/13/2012
对不起,您没有驱动编译器进行计算500500
1赞 benmmurphy 8/13/2012
这是指所有三种解决方案吗?在解决方案 1 中,我获取了一些 Java 代码并对其进行编译,编译器打印出 500500。这看起来很像计算 500500 的编译器。这怎么不是编译器计算 500500?
0赞 Xeo 8/13/2012
是的,没错,我说的是解决方案 2 和 3。我已经在之前的更新中阅读了这个答案,然后又回到了最新的更新中,不知何故似乎忘记了第一个解决方案。
0赞 benmmurphy 8/16/2012
我会说解决方案 2 和 3 也在计算它。您可以添加任意数量的检查,因此您基本上正在做.我有一个 160MB 的 Java 文件可以做到这一点,它可以:)for (i = 0; i < 100000; ++i) {if (i == 1000*1000/2) print i}
7赞 milleniumbug 12/19/2012 #12

从 Carl Walsh 的回答扩展到在编译过程中实际打印结果:

#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'
1赞 ebasconp 3/13/2014 #13

虽然这实际上适用于小数字,但如果我使用的是 N > 400 sum_first,clang++ 会向我返回编译器错误。

#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;
}