5

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?

2
  • FWIW, the provided example (after fixing the missing #include) compiles without warning with G++ 4.6.1. Commented Oct 19, 2011 at 16:15
  • It is specific for Visual C++ 2010. Commented Oct 19, 2011 at 16:18

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

It is a simplified code, to understand the problem. In the real case, I need capturing scope to access some data members (declared, hence initialized prior to the action_ member). I also suspect bad implementation of lambdas...
[&] will capture this, and by that means, all member variables (including itself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.