title: Can a C++11 lambda capture a file scope variable?
body: <p>Section 5.1.2 p10 of the ISO C++11 specification states:</p>
  
  <blockquote>
    <p>The identifiers in a capture-list are looked up using the usual rules
    for unqualified name lookup (3.4.1); each such lookup shall find a
    variable with automatic storage duration declared in the reaching
    scope of the local lambda expression. An entity (i.e. a variable or
    this) is said to be explicitly captured if it appears in the
    lambda-expression’s capture-list.</p>
  </blockquote>
  
  <p>This seems to imply that a lambda cannot capture a file scope variable. For example, this program should be illegal:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int x = 13;
  
  int main()
  {
    auto l = [](){ return x; };
  
    std::cout &lt;&lt; l() &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>However, <code>g++</code> 4.7.1 produces the result I expect:</p>
  
  <pre><code>$ g++ --version
  g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
  Copyright (C) 2012 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  
  $ g++ -std=c++11 lambda.cpp 
  $ ./a.out 
  13
  </code></pre>
  
  <p>But <code>clang</code> 3.0 crashes:</p>
  
  <pre><code>$ clang --version
  Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
  Target: i386-pc-linux-gnu
  Thread model: posix
  $ clang -std=c++11 lambda.cpp 
  0  libLLVM-3.0.so.1 0xb70a59e8
  1  libLLVM-3.0.so.1 0xb70a5f34
  2                   0xb775d400 __kernel_sigreturn + 0
  3  clang            0x0869f2e9 clang::Sema::DeduceAutoType(clang::TypeSourceInfo*, clang::Expr*, clang::TypeSourceInfo*&amp;) + 73
  &lt;snip&gt;
  </code></pre>
  
  <p>Is my program illegal or not? If it is illegal, what is the rationale for the proscription on capturing file scope variables?</p>