Cătălin Pitiș
Cătălin Pitiș

Reputation: 14341

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?

Upvotes: 5

Views: 520

Answers (1)

Michael Price
Michael Price

Reputation: 8978

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?

Upvotes: 1

Related Questions