Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error in template class with lambda expression

I have the following simplified scenario:

template< typename T>
struct A
{
  A() : action_( [&]( const T& t) { })
  {}

private:
   boost::function< void( const T& )> action_;
};

When compiling with Visual C++ 2010, it gives me a syntax error at construction of action_:

1>test.cpp(16): error C2059: syntax error : ')'
1>          test.cpp(23) : see reference to class template instantiation A<T>' being compiled

What is strange is that the same example, with no template parameter, compiles just fine:

struct A
{
  A() : action_( [&]( const int& t) { })
  {}

private:
  boost::function< void( const int& )> action_;
};

I know that one workaround to the problem is to move the action_ initialization in the constructor body, instead of initialization list, like in the code below:

template< typename T>
struct A
{
  A()
  {
    action_ = [&]( const T& t) { };
  }

private:
  boost::function< void( const T& )> action_;
};

... but I want to avoid such workaround.

Did anybody encountered such situation? Is any explanation/solution to this so called syntax error?

like image 741
Cătălin Pitiș Avatar asked Nov 13 '22 15:11

Cătălin Pitiș


1 Answers

Broken implementation of lambdas in Visual C++ 2010? That's my best guess for an explanation.

Although, I'm intrigued what capturing scope variables by reference does in this situtation... Nothing?

like image 57
Michael Price Avatar answered Jan 09 '23 04:01

Michael Price