64

Example:

#include <functional>

int main() {
  auto test = []{};
  test = []{};
    
  return 0;
}

This emits the following error message in gcc 4.7.2:

test.cpp: In function ‘int main()’:
test.cpp:5:13: error: no match for ‘operator=’ in ‘test = <lambda closure object>main()::<lambda()>{}’
test.cpp:5:13: note: candidate is:
test.cpp:4:16: note: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
test.cpp:4:16: note:   no known conversion for argument 1 from ‘main()::<lambda()>’ to ‘const main()::<lambda()>&’

From the standard 5.1.2.3 (emphasis mine):

An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:

— the size and/or alignment of the closure type,

— whether the closure type is trivially copyable (Clause 9)

— whether the closure type is a standard-layout class (Clause 9), or

— whether the closure type is a POD class (Clause 9).

As far as I can tell, this is what I'm running up against. It's attempting to use a deleted assignment operator and failing. I am curious to know if there's an easy workaround, and more broadly what the motivating rationale for allowing copy constructibility to be omitted for lambdas generally.

12
  • It's not attempting to use the copy constructor. It's attempting to use the assignment operator. Commented Sep 12, 2013 at 5:17
  • 11
    No, what's preventing you from using the assignment operator is 5.1.2.20 The closure type associated with a lambda-expression has a deleted (8.4.3) default constructor and a deleted copy assignment operator. It has an implicitly-declared copy constructor (12.8) and may have an implicitly-declared move constructor (12.8). (well, that, and the fact that the type of the lambda is different) Commented Sep 12, 2013 at 5:37
  • 21
    If you put a + before the first lambda, it magically starts to work. Commented Sep 14, 2013 at 11:12
  • 2
    @OmnipotentEntity it's pure magic, I have no idea how it works! Does the + operator enable some special feature!? Commented Sep 16, 2013 at 21:03
  • 4
    @JohannesSchaub-litb I think I solved the puzzle. It was a bit long for a comment, so I made it a self-answer here. Commented Sep 19, 2013 at 7:52

5 Answers 5

71

You seem to think that those two lambdas have the same type, but that is not true. Each one creates its own type:

#include <functional>
#include <type_traits>
#include <iostream>

int main() {
  auto test = []{};
  auto test2 = []{};
  std::cout << std::is_same< decltype( test ), decltype( test2 ) >::value << std::endl;
  return 0;
}

will print 0. Of course the error message you are getting from the compiler could be a little bit clearer in this regards...

7
  • 25
    Reference: [expr.prim.lambda]/3 "The type of the lambda-expression [...] is a unique, unnamed non-union class type — called the closure type"
    – dyp
    Commented Sep 12, 2013 at 5:30
  • @DyP Was about to search for it in the standard but you were faster. Thanks :) Commented Sep 12, 2013 at 5:31
  • 11
    I don't know, the usage of the word unique pretty much shouts it. ;) Thanks! Commented Sep 12, 2013 at 5:33
  • 1
    Pretend that each lambda expression is a macro that replaces the expression with an unnamed class definition with an overloaded operator(), and member variables that hold captured variables. Even if the structure of two classes are the same, they are still different types. Commented Sep 12, 2013 at 5:39
  • 1
    Even if they were the same type you still would not be able to copy them because the copy constructor is deleted (see section 5.1.2.20 of the C++11 standard). The compiler is not even getting to the point where it needs to check if the types match before it fails. This is the reason why you are getting the "no match for operator=" error. See the example I give in my answer.
    – Rastaban
    Commented Jan 3, 2014 at 3:13
47

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non- union class type

So it is like you are doing the following:

struct {} a;
struct {} b;
a = b; // error, type mismatch

Use std::function if you want to assign different lambdas with the same signature to the same variable.

std::function<void()> f = []{};
f = []{}; //ok
4
  • 6
    Or, since they're non-capturing, void(*f)() will do too. This can be more efficient.
    – MSalters
    Commented Sep 12, 2013 at 7:12
  • 8
    The type is unique because the type encodes which particular function to call; the function is simply operator() of the type. This allows inlining the function in generic algorithms which is not possible with either function pointers or std::function.
    – Jan Hudec
    Commented Sep 12, 2013 at 9:04
  • @JanHudec: Sort of, even if two lambda expressions are identical, they are still distinct types. Commented Sep 12, 2013 at 16:24
  • @user1131467: If you have two classes with the same content, they are still distinct types too.
    – Jan Hudec
    Commented Sep 13, 2013 at 6:47
8

Lambda can't be redefined because each lambda is of a different, anonymous, incompatible type. They can be copied only if you pass them to a templated function (like std::function ctor) that would be able to deduce that type.

6

The reason you are not able to do this is because the copy assignment operator for the lambda-expression is declared deleted, See section 5.1.2/20 of the standard. For a more clear (for unusual definitions of clear) see this code sample

template<class T> void f(T x1)
{
  T x2 = x1; // copy constructor exists, this operation will succeed.
  x2 = x1; // assignment operator, deleted and will cause an error
}
int main()
{
  f([]{});
  return 0;
}

Other answers have pointed out that each lambda has a unique type, but this is not the reason why you are getting that error. This example shows that even if the two lambdas have the same type, it still is not able to copy it. However you are able to copy it to a new variable. This is the reason your error message is complaining about missing operator= and not about their types being different. Although each lambda having it's own type does not help you out much either.

6
  • +1. You should add the quote from the Standard (which is 5.1.2/19, not 5.1.2/20 by the way), to the answer as well.
    – jogojapan
    Commented Jan 3, 2014 at 3:29
  • 1
    @jogojapan Sorry, but this answer is wrong. Look at the OPs error message: The problem is the different type, although the message says main()::<lambda()> twice. Newer versions of GCC fixed the message. See also my comment to Rastaban's comment on my answer. Commented Jan 3, 2014 at 8:26
  • @DanielFrey Oh I see. Thanks for pointing this out. (But shouldn't 5.1.2/19 still be mentioned in a complete answer? It's true that a lambda expression has a unique type, but the wording in 5.1.2/3 suggests the type is unique for the lambda expression, not for the closure object. Hence if the same expression []{} is used twice in the same scope, the type could still be identical. Moreover, even if the type was different, implicit conversion might be possible..?)
    – jogojapan
    Commented Jan 3, 2014 at 10:41
  • 1
    @jogojapan 5.1.2/3 refers to "the lambda-expression", meaning a single occurrence of lambda-expression in the source. I therefore read it as saying that the "unique" in this sentence means that each occurrence in the source creates a unique type, even if the same token sequence occurs twice it's still two occurrences and each one has its own type. Also, there is no implicit conversion for those types and no other conversions are defined that could match here. Commented Jan 3, 2014 at 10:49
  • @jogojapan ...continued: But even though this answer doesn't really explains the OPs immediate problem, I think it's worth to keep it here. It shows that besides the underlying problem I addressed in my answer, he was making another thinko by assuming that even though lambda's are copyable, they are still not assignable in any case. Therefore: +1 for Rastaban :) Commented Jan 3, 2014 at 10:51
2

If we could assign one lambda to another lambda of a different type, how do we copy the function bodies/definitions from that lambda to the other one? If we would be so stubborn, then we could use some member std::function-like type to be the one who will be copied. But that would be against the ol' C++ rule of not paying blah blah...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.