title: Can't compile basic C++ program with TBB and lambda
body: <p>I tried to write a basic C++ program using TBB and lambda expression, but I can't compile it.
  <code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;tbb/parallel_for.h&gt;
  #include &lt;tbb/blocked_range.h&gt;
  
  using namespace std;
  using namespace tbb;
  
  void Foo(int number) {
      cout&lt;&lt;number&lt;&lt;endl;
  }
  
  void ParallelApplyFoo(int* a, size_t n) {
      parallel_for(blocked_range&lt;size_t &gt; (0, n),
              [ = ](const blocked_range&lt;size_t&gt;&amp; r){
          for (size_t i = r.begin(); i != r.end(); ++i)
                  Foo(a[i]);
          }
      );
  }
  
  int main(int argc, char** argv) {
      int num = 10;
      int* a = new int[num];
      for(int i = 0; i &lt; num; i++)
          a[i] = i;
      ParallelApplyFoo(a,num);
      return 0;
  }
  </code></pre>
  
  <p></code></p>
  
  <p>And compiler messages:</p>
  
  <pre>
  main.cpp: In function ‘void ParallelApplyFoo(int*, size_t)’:
  main.cpp:25:9: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
  main.cpp:26:5: error: no matching function for call to ‘parallel_for(tbb::blocked_range, ParallelApplyFoo(int*, size_t)::&)>)’
  main.cpp:26:5: note: candidates are:
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:161:6: note: template void tbb::parallel_for(const Range&, const Body&)
  main.cpp:26:5: error: template argument for ‘template void tbb::parallel_for(const Range&, const Body&)’ uses local type ‘ParallelApplyFoo(int*, size_t)::&)>’
  main.cpp:26:5: error:   trying to instantiate ‘template void tbb::parallel_for(const Range&, const Body&)’
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:168:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::simple_partitioner&)
  /usr/include/tbb/parallel_for.h:168:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 3 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:175:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::auto_partitioner&)
  /usr/include/tbb/parallel_for.h:175:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 3 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:182:6: note: template void tbb::parallel_for(const Range&, const Body&, tbb::affinity_partitioner&)
  /usr/include/tbb/parallel_for.h:182:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 3 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:190:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::simple_partitioner&, tbb::task_group_context&)
  /usr/include/tbb/parallel_for.h:190:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 4 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:197:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::auto_partitioner&, tbb::task_group_context&)
  /usr/include/tbb/parallel_for.h:197:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 4 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:204:6: note: template void tbb::parallel_for(const Range&, const Body&, tbb::affinity_partitioner&, tbb::task_group_context&)
  /usr/include/tbb/parallel_for.h:204:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   candidate expects 4 arguments, 2 provided
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:248:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, const Function&, tbb::task_group_context&)
  /usr/include/tbb/parallel_for.h:248:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:235:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, Index, const Function&, tbb::task_group_context&)
  /usr/include/tbb/parallel_for.h:235:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:228:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, const Function&)
  /usr/include/tbb/parallel_for.h:228:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)
  In file included from main.cpp:10:0:
  /usr/include/tbb/parallel_for.h:215:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, Index, const Function&)
  /usr/include/tbb/parallel_for.h:215:6: note:   template argument deduction/substitution failed:
  main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)
  gmake[2]: *** [build/Release/GNU-Linux-x86/main.o] Error 1
  </pre>
  
  <p>I tried compiler flag "-std=c++11" and "-std=gnu++11" which outputs then:</p>
  
  <pre>
  g++ -std=gnu++11    -o dist/Release/GNU-Linux-x86/test build/Release/GNU-Linux-x86/main.o  
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::~start_for()':
  main.cpp:(.text+0x6): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::~start_for()':
  main.cpp:(.text+0x26): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::run(tbb::blocked_range const&, {lambda(tbb::blocked_range const&)#1} const&, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1} const&)':
  main.cpp:(.text+0x9d): undefined reference to `tbb::task_group_context::init()'
  main.cpp:(.text+0xb5): undefined reference to `tbb::internal::allocate_root_with_context_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0xe1): undefined reference to `tbb::internal::get_initial_auto_partitioner_divisor()'
  main.cpp:(.text+0x107): undefined reference to `tbb::task_group_context::~task_group_context()'
  main.cpp:(.text+0x116): undefined reference to `tbb::task_group_context::~task_group_context()'
  main.cpp:(.text+0x12a): undefined reference to `vtable for tbb::task'
  main.cpp:(.text+0x138): undefined reference to `tbb::internal::allocate_root_with_context_proxy::free(tbb::task&) const'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::execute()':
  main.cpp:(.text+0x251): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x27b): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x313): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x33d): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x4b6): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x4e8): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'
  main.cpp:(.text+0x585): undefined reference to `tbb::task_group_context::is_group_execution_cancelled() const'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::flag_task::~flag_task()':
  main.cpp:(.text._ZN3tbb10interface68internal9flag_taskD2Ev[_ZN3tbb10interface68internal9flag_taskD5Ev]+0x6): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::signal_task::~signal_task()':
  main.cpp:(.text._ZN3tbb10interface68internal11signal_taskD2Ev[_ZN3tbb10interface68internal11signal_taskD5Ev]+0x6): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::signal_task::~signal_task()':
  main.cpp:(.text._ZN3tbb10interface68internal11signal_taskD0Ev[_ZN3tbb10interface68internal11signal_taskD0Ev]+0x6): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::flag_task::~flag_task()':
  main.cpp:(.text._ZN3tbb10interface68internal9flag_taskD0Ev[_ZN3tbb10interface68internal9flag_taskD0Ev]+0x6): undefined reference to `vtable for tbb::task'
  build/Release/GNU-Linux-x86/main.o:(.rodata+0x20): undefined reference to `typeinfo for tbb::task'
  build/Release/GNU-Linux-x86/main.o:(.rodata._ZTVN3tbb10interface68internal11signal_taskE[_ZTVN3tbb10interface68internal11signal_taskE]+0x14): undefined reference to `tbb::task::note_affinity(unsigned short)'
  build/Release/GNU-Linux-x86/main.o:(.rodata._ZTVN3tbb10interface68internal9flag_taskE[_ZTVN3tbb10interface68internal9flag_taskE]+0x14): undefined reference to `tbb::task::note_affinity(unsigned short)'
  build/Release/GNU-Linux-x86/main.o:(.rodata._ZTIN3tbb10interface68internal11signal_taskE[_ZTIN3tbb10interface68internal11signal_taskE]+0x8): undefined reference to `typeinfo for tbb::task'
  build/Release/GNU-Linux-x86/main.o:(.rodata._ZTIN3tbb10interface68internal9flag_taskE[_ZTIN3tbb10interface68internal9flag_taskE]+0x8): undefined reference to `typeinfo for tbb::task'
  </pre>
  
  <p>I am using Fedora 18 (tbb and tbb-devel packages installed of course).</p>
  
  <p>Anyone some ideas please? The code is copied from here: <a href="http://software.intel.com/en-us/blogs/2009/08/03/parallel_for-is-easier-with-lambdas-intel-threading-building-blocks" rel="nofollow">http://software.intel.com/en-us/blogs/2009/08/03/parallel_for-is-easier-with-lambdas-intel-threading-building-blocks</a></p>