fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3. #include <cstdlib>
  4.  
  5. int main() {
  6. const int m = 42;
  7. [] {std::cout<<m<<std::endl; }(); // OK
  8.  
  9. const int n = std::rand();
  10. [] { std::cout<<n<<std::endl; }(); // error: 'n' is not captured
  11.  
  12. return 0;
  13. }
Compilation error #stdin compilation error #stdout 0s 16064KB
stdin
Standard input is empty
compilation info
prog.cpp: In lambda function:
prog.cpp:10:21: error: ‘n’ is not captured
     [] { std::cout<<n<<std::endl; }(); // error: 'n' is not captured
                     ^
prog.cpp:10:6: note: the lambda has no capture-default
     [] { std::cout<<n<<std::endl; }(); // error: 'n' is not captured
      ^
prog.cpp:9:15: note: ‘const int n’ declared here
     const int n = std::rand();
               ^
stdout
Standard output is empty