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

[Bug report] Cannot convert non-capturing lambda closure to function pointer using the unary + operator

Alberto_L_
Beginner
633 Views

I've come up with an issue when trying to use some generic code involving lambda functions. Here it is a minimal code to reproduce it:

#include <iostream>
int main() {
    auto f = [] { std::cout << "this is a test" << std::endl; };
    void(*fp0)() = f; // OK
    auto fp1 = +f;    // Does not compile
    fp0();
    fp1();
    return 0;
}

And this is the error the compiler raises:

error: more than one conversion function from "lambda []()->void" to a built-in type applies:
            function "lambda []()->void::operator void (*)()() const"
            function "lambda []()->void::operator void (*)()() const"
      auto fp1 = +f;    // Does not compile

The main problem with this bug is that it makes imposible to convert lambdas to regular function pointers in a generic context when you don't know the signature beforehand. I've tested the given code with another compilers and it just works. Currently I'm using Intel Parallel Studio XE 2019 Update 2.

0 Kudos
4 Replies
Viet_H_Intel
Moderator
633 Views

What other compilers you have tested? and what Microsoft VS version are you using?

0 Kudos
Alberto_L_
Beginner
633 Views

I've tested it also with gcc, clang, apple-clang and msvc.

In the case of gcc and clang, the example works with fairly old versions of them. gcc is able to compile it since its version 4.7 and clang since its version 3.5.

msvc had also the same problem, it works since version 19.14 onwards.

I am running icl directly from the command line, not from Visual Studio; but in case it is helpful, the current version of VS I have installed is VS 2017 15.9.

0 Kudos
Viet_H_Intel
Moderator
633 Views

Can you please provide g++'s command line options?

0 Kudos
Alberto_L_
Beginner
633 Views

I've only provided the c++ standard flag (the code compiles with 11, 14 and 17). E.g:

g++ main.cpp -std=c++11

 

0 Kudos
Reply