0

If I write this

std::function<int()> myFunction = []() {return 42;};

in C++/CLI under VS2010 (in an MSTest unit test class, in case it matters), I get the compiler error C3809: a managed type cannot have any friend functions/classes/interfaces.

Is a lambda considered as a friend because it cooooould access local variables, even if it doesn't?

On the other hand, this works fine:

int ThatOldJokeAgain()
{
    return 42;
}
[...]
std::function<int()> myFunction2 = ThatOldJokeAgain;

Is there a way to make lambdas work in C++/CLI? Or is my error something completely different?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Robin
  • 518
  • 3
  • 13
  • @Joachim: Yes, it does: http://blogs.msdn.com/b/vcblog/archive/2008/10/28/lambdas-auto-and-static-assert-c-0x-features-in-vc10-part-1.aspx#9034197 – Robin Jun 25 '15 at 12:05
  • @Matteo: That question is about .Net lambdas (syntactic sugar for delegates), not the C++0x lambdas. Also, I'm asking this question because I have found nothing about this "friend" error message and lamba expressions. – Robin Jun 25 '15 at 12:05
  • sorry, I pasted the wrong link in the comment; previously I found another answer which had a working link to some official source stating that they were not supported in C++/CLI, but I can't seem to find it again. Anyhow, the duplicate linked at the top is exactly your problem (down to the exact error message). – Matteo Italia Jun 25 '15 at 12:13
  • @Matteo: Thank you, that is exactly it! Sorry, I didn't find it before asking. – Robin Jun 25 '15 at 12:37

0 Answers0