Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Lambda::operator=()

Dmitry_Vyukov
Valued Contributor I
431 Views

Intel C++ Compiler 11.1.071 [IA-32]

Following code compiles successfully:

[bash]int main()
{
    int i = 0;
    auto f1 = [=](){printf("%u", i);};
    auto f2 = f1;
    f2 = f1;
}
[/bash]
I expect it to NOT compile. According to C++0x ISO draft closure type associated with lambda expression has deleted copy assignment operator. So I expect to get "closure has deleted/inaccessible assignment operator" error.

Similarly, in the following code:

[bash]int main()
{
    int i = 0;
    auto f1 = [&](){printf("%u", i);};
    auto f2 = f1;
    f2 = f1;
}
[/bash]

I get following error:

.\\test.cpp(11): error: implicitly generated assignment operator cannot copy:
reference member "__lambda1::i"
auto f1 = [&](){printf("%u", i);};
^
detected during implicit generation of "__lambda1 &__lambda1::operator=(const __lambda1 &)" at line 13

I expect to get "closure has deleted/inaccessible assignment operator" error instead. It does not matter as to whether individual captured objects have accessible assignment operator or not, the closure itself must be not copyable by means of assignment operator.


0 Kudos
3 Replies
Judith_W_Intel
Employee
431 Views

You are correct. In our next compiler release you will see the same diagnostic for both examples, i.e.:

sptel5-113> cat lambda1.cpp
#include

int main()
{
int i = 0;
auto f1 = [=](){printf("%u", i);};
auto f2 = f1;
f2 = f1;
}

sptel5-114> icpc -std=c++0x -c lambda1.cpp
lambda1.cpp(8): error: function "lambda []()->void::operator=(const lambda []()->void &)" (declared at line 6) cannot be referenced -- it is a deleted function
f2 = f1;
^

compilation aborted for lambda1.cpp (code 2)
sptel5-115>

0 Kudos
Dmitry_Vyukov
Valued Contributor I
431 Views

Ok, thank you.

Btw, will the next release support deleted functions? Or it's just a special handling of lambdas?

0 Kudos
Judith_W_Intel
Employee
431 Views

Yes, our next release will support defaulted and deleted functions (under the -std=c++0x switch).

Here is a description of the change:

Defaulted and deleted functions

In C++0x mode, the front end now implements deleted functions ("= delete;")
and defaulted special member functions ("= default;"). A deleted function is
a function declaration that cannot be referenced. For example:

int f(int) = delete;
short f(short);
int x = f(3); // Error: selected function is deleted.
int y = f((short)3); // Okay.

Special member functions that can be implicitly defined can instead be
explicitly declared but with a "default definition". As the following example
shows, the default definition can be specified inside or outside the enclosing
class definition, but only if it appears inside the class can that class be a
POD type (if all other constraints for POD types are fulfilled).

struct S { S(S const&) = default; };
struct T { T(T const&); };
T::T(T const&) = default;
static_assert(__is_pod(S), "S is a POD");
static_assert(!__is_pod(T), "T is not a POD");

This change was voted into the C++ committee's working paper for the next
standard in July 2007 (meeting in Toronto, through proposal numbered N2346).

0 Kudos
Reply