11

I have a class with a std::function constructor parameter.

class ClazzA{
public:
    ClazzA(function<void()> foo){}
    ClazzA(){
        ClazzA([](){});
    }
};

If I have an instance of this class as a member of another, I have to call constructor in initializer list. I can pass a lambda as an argument, and it is automatically converted:

class ClazzB{
public:
    ClazzA a;
    ClazzB() :
      // works fine:
      a([](){}){}
};

But if ClazzB is a template, lambda doesn't work:

template<typename T> class ClazzC{
public:
    ClazzA a;
    //works fine:
    ClazzC(function<void()> foo) : a(foo){}
    //doesn't work:
    ClazzC() :
      //syntax error : ')'
      a([](){})
      //syntax error : '{'
      //unexpected token(s) preceding '{'; skipping apparent function body
      {}
};

The compiler is MSVC++ 2010. I don't understand what I am doing wrong or why this syntax is not supported.

At first ClazzA was a template too, and function was a bit more complex, so I thought it was the problem with templated lambda or something. But after I removed all that code the problem remains.

UPD: Tried to compile in MinGW G++, it works. Looks like a Visual Studio issue.

12
  • do not write "Class" prefix for class names, the code will be more clear Commented Jul 8, 2011 at 12:39
  • 4
    VC++2010 implements an older more limited version of lambdas. They have a number of subtle incompatibilities with the latest C++0x draft. You may be running into a problem due to this, but I'm not 100% certain. Commented Jul 8, 2011 at 12:44
  • 4
    The code seems correct and GCC accepts it. Commented Jul 8, 2011 at 12:48
  • 1
    @KennyTM: Forget about it. I fliped the [] with (), guess I could not see the right brackets anymore. Written correctly it works on the gcc but still fails on VS2010. Commented Jul 8, 2011 at 16:09
  • 4
    @aimozg Welcome to Stack Overflow! If you've solved your problem on your own, please post your answer and accept it here so that this question doesn't continue to show up as "unanswered". Commented Jul 10, 2011 at 21:16

1 Answer 1

2

This is a MSVS C++0x implementation problem (see comments under question). Problem solved.

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

1 Comment

Guess this link: cpprocks.com/… could be useful to those looking at lambda related bugs in VC2010

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.